[英]iframe source from URL
我们的网站上有一个页面,它加载了一个afillate商店的iFrame。 我们希望发送一封电子邮件,其中包含可在我们的网站上加载页面的链接,其中包含iFrame中产品的详细信息。 我下面的代码是有效的,但是如果用户直接访问我们网站上的页面 - 而不是使用URL中定义的页面元素的链接,它就会中断。
不确定它是否重要,但iframe源是在一个完全不同的域上,我无法控制它。
这有效: http://www.mysite.html/thispage.aspx?id = 16500&page = http://myiframesource.com/detail/0307959473
这不起作用: http://www.mysite.html/thispage.aspx?id = 16500 (iframe src更改为“undefined”onload)
这是我到目前为止所拥有的:
<script language="javascript">
window.onload = updateiframe; //event handler (calls the function)
function updateiframe() {
function getUrlVars() {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
var hash = getUrlVars();
var myiframepage = decodeURIComponent(hash['page']);
document.getElementById('myiframe').src = myiframepage;
}
</script>
<iframe id="myiframe" name="myiframe" src="myiframesource.com" width="555" height="2000" border="0" style="border: 0px solid #ffffff;"></iframe>
在此先感谢您的帮助!
无法使用的网址错过了“page”参数。
您的javascript正在从URL中获取所有参数并将它们放入数组中。 接下来它尝试在该数组中找到'page'参数:
var myiframepage = decodeURIComponent(hash['page']);
这显然是缺失的。 因此,iframe的“未定义”来源。
你可以在你的javascript中添加一小段来检查正确的'id'参数,如果是16500并且你缺少'page'参数,你可以进行javascript重定向:
var hash = getUrlVars();
var myiframepage = decodeURIComponent(hash['page']);
if(typeof(myiframepage) == 'undefined') {
var id = decodeURIComponent(hash['id']);
if(typeof(id) != 'undefined' && id == '16500') {
window.location.replace('http://www.mysite.html/SiteText.aspx?id=16500&page=http://myiframesource.com/detail/0307959473');
}
}
else {
document.getElementById('myiframe').src = myiframepage;
}
上面显示的这一小段代码应该照顾那些没有电子邮件链接并直接浏览页面的人...
并且要小心传递这样的数据。 通过提供具有不同页面参数的URL,人们可以轻松地劫持您的iframe并在其中显示恶意网站!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.