我想在用户访问网站时自动弹出一个URL,但是在具有sandbox="allow-popups"
属性的iframe中执行此操作不起作用。
我的代码是这样的:
<!-- add an iframe ad in target.html -->
<iframe sandbox="allow-scripts allow-popups"
style="vertical-align:middle; margin:10px 0" frameborder="0"
scrolling="no" src="//aaa.html" width="414" height="82.8">
</iframe>
AAA.html是这样的:
<html><head>
<meta charset="UTF-8">
<meta name="applicable-device" content="mobile">
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1">
</head>
<body style="margin: 0;">
<script>
(function() {
var d = "https://xxxx.com";
var f=d;
var a = new XMLHttpRequest();
var n=
f +
"/" +
"plug.php?s=1415";
if (a != null) {
a.onreadystatechange = function() {
if (a.readyState == 4 && a.status == 200) {
if (window.execScript)
window.execScript(a.responseText, "JavaScript");
else if (window.eval)
window.eval(a.responseText, "JavaScript");
else eval(a.responseText);
}
};
a.open("GET", n, false);
a.send();
}
})();
</script>
</body></html>
我想要的是当用户访问目标.html时,浏览器每次都会自动弹出aaa.html。
这有可能实现吗?
好吧,我不确定您的所有脚本都做了什么,但是,它无法更改 https://platform.twitter.com/widgets/tweet_button.html 页面上的内容。它可以向它传递一个查询字符串,https://platform.twitter.com/widgets/tweet_button.html 上的代码可以基于此字符串(如果存在(更改自身。
当我第一次回复时,我很着急。这里有一个更详细的解释。把它放在你的 https://platform.twitter.com/widgets/tweet_button.html 头上,包含 iframe 的那个:
<script type="text/javascript">
/*Load iframe from Query script
*As first seen in http://www.dynamicdrive.com/forums
*This notice must remain for legal use */
function loadframe(){
if(window.location.replace)
window.frames.daFrame.location.replace(get('framepage'));
else
window.frames.daFrame.location.href=get('framepage');
}
function get(key_str) {
var query = window.location.search.substr(1);
var pairs = query.split("&");
for(var i = 0; i < pairs.length; i++) {
var pair = pairs[i].split("=");
if(unescape(pair[0]) == key_str)
return unescape(pair[1]);
}
return null;
}
if (location.search&&get('framepage')!=null)
if ( typeof window.addEventListener != "undefined" )
window.addEventListener( "load", loadframe, false );
else if ( typeof window.attachEvent != "undefined" )
window.attachEvent( "onload", loadframe );
else {
if ( window.onload != null ) {
var oldOnload = window.onload;
window.onload = function ( e ) {
oldOnload( e );
loadframe();
};
}
else
window.onload = loadframe;
}
</script>
注意两个红色的 daFrame。用你的 iframe 的名称替换它们。
在要包含在 iframe 中的页面头部使用这样的脚本:
<script type="text/javascript">
function load_content (page) {
if (window.location==top.location)
if (window.location.replace)
top.location.replace(page+'?framepage='+top.location.href);
else
top.location.href=page+'?framepage='+top.location.href;
}
</script>
并且,在其正文标签中:
网页代码:
<body onload="load_content('https://platform.twitter.com/widgets/tweet_button.html');">