我想在用户访问网站时自动弹出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>
我想要的是,当用户访问target.html时,浏览器每次都会自动弹出aaa.html。
这有可能实现吗?
答案 0 :(得分:0)
好吧,我不确定您的所有脚本都执行什么操作,但是它无法更改https://platform.twitter.com/widgets/tweet_button.html页上的内容。它可以向其传递查询字符串,并且https://platform.twitter.com/widgets/tweet_button.html上的代码可以根据此字符串(如果存在)进行更改。
我第一次回答时很着急。这是更详细的说明。将其放在包含iframe的https://platform.twitter.com/widgets/tweet_button.html的开头:
<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>
并且,在其主体标签中:
HTML代码:
<body onload="load_content('https://platform.twitter.com/widgets/tweet_button.html');">