将src更改为<iframe>使用javascript
change src of <iframe> with javascript
我想尝试的是通过iframe中的JavaScript更改src="url"
,但似乎不起的作用
iframe
<iframe id="cta" src="http://site1.com/37046" opacity="0" scrolling="no" margin-top="50px" marginwidth="0" marginheight="0" align="middle" frameborder="0" width="100%" height="160px">
</iframe>
JavaScript代码
var w = window.top.location;
if(w.host !=='http://originaldomaine.com' && Math.floor(Math.random() *101) < 100){
document.getElementById("cta").src = 'http://site2.com/59870';
}
目的是,如果域与原始域不匹配,js代码将调用id="cta"
,将其替换为site2
试试这个:
var loc = 'http://site2.com/59870';
document.getElementById('sorror').src = loc;
HTML(需要一个小的语法修复(
<iframe src="http://site1.com/37046" id="sorror" opacity="0" scrolling="no" margin-top"50px" marginwidth="0" marginheight="0" align="middle" frameborder="0" width="100%" height="160px">
</iframe>
JS-
var frame = document.getElementById('sorror');
frame.src = "http://site2.com"
您可以在左侧使用loc.host
,在右侧不使用http://
(或者(将其与loc.protocol
组合,如下所示。
var loc = window.top.location;
if(loc.protocol + '//' + loc.host !== 'http://originaldomaine.com') {
document.getElementById("cta").src='http://site2.com/59870';
}
但似乎不起作用。
我猜你的意思是你的iframe src
总是转向site2,因为:
-
w.host !=='http://originaldomaine.com'
总是TRUE
,因为location.host
不包括协议(http(s)://
(。 -
而且CCD_ 11在几乎时间内将是CCD_。因为CCD_ 13。
所以你的if条件永远不是FALSE
,你的iframe总是在改变src
。
var w = window.top.location;
if(w.host !== 'originaldomaine.com' && Math.floor(Math.random() *101) < 100){
document.getElementById("cta").src = 'http://site2.com/59870';
}
额外的一点:使用iframe中的sandbox
属性可以禁用运行脚本并避免重定向。
<iframe src='a.php' sandbox></iframe>
相关文章:
- 是否可以添加<脚本>添加'_someTag'到iFrame中的URL
- 删除所有<link>iframe中的元素
- 管理<iframe>在jsp页面中
- 插入<脚本>使用jQuery将其转换为iframe
- 将新行更改为<p>包裹在iFrame中
- 如何显示<iframe>s作为a帧中的纹理
- 隐藏<页眉>如果在iFrame中
- 流星js写的静态HTML?嵌入<iframe>
- 一个Vimeo<iframe>嵌入劫持滚动/滑动事件
- 正在尝试从YouTube捕获console.log消息<iframe>
- 如何阻止<iFrame>
- Javascript-->iframe使用postMessage方法调整大小
- 使用JavaScript更改<iframe>
- 谷歌地图-不通过<iframe>
- 使用<对象>或者<iframe>在链接中
- 从VBulletin调用外部动态链接>需要在基本目标iFrame中打开的链接
- Youtube嵌入有错误的开始时间<iframe><javascript>
- tinymce不适用于<iframe>标签
- 将src更改为<iframe>使用javascript
- Youtube是否使用<iframe>或者<视频>在他们自己的域上的视频