iframe 点击事件更改位置/URL(跨域但可以控制两个域)
我正在开发一个网站,该网站由主网站组成,然后是分支网站。
这些活动位于单独的域中(但我可以访问这两个域)。
活动通过 iframe 拉入主网站,每个 iframe 将包含主网站菜单(通过代理拉入活动网站)。
我需要的是,如果单击主导航上的链接,实际的浏览器位置将发生变化,而不仅仅是 iframe。这样浏览器 URL 就会发生变化,用户实际上会通过 iframe 离开页面。
我知道相同的起源政策,这就是为什么我遇到这么多麻烦的原因 - 但由于我可以控制两者并且可以修改每个脚本,我认为必须有办法解决这个问题?
我一直在尝试类似的事情
$(function(){
$('#menu a').click(function(){
var linkLocation = $(this).attr('href');
//parent.location.href=linkLocation
parent.location.replace(linkLocation)
return false;
});
});
,但无论其放置在哪个域中,它似乎都不起作用..非常感谢任何指针/帮助/建议。
安迪
I'm working on a site which comprises off the main site - then campaign sites which are off-shoots.
The campaigns are on a separate domain (but I have access to both).
The campaigns are pulled into the main site via an iframe and each iframe will contain the main sites menu (this gets pulled to the campaign site via a proxy).
What I need is that if a link on the main navigation gets clicked the actual browser location will change rather than just the iframe. So that the browsers URL changes and the user actually leaves the page with the iframe.
I'm aware of the same origin policy which is why I'm having so much trouble - BUT as I have control of both and can modify scripts on each I figure there must be a way around this?
I've been trying things like
$(function(){
$('#menu a').click(function(){
var linkLocation = $(this).attr('href');
//parent.location.href=linkLocation
parent.location.replace(linkLocation)
return false;
});
});
but it doesn't seem to work regardless of domain its placed on.. any pointers / help / suggestions greatly appreciated.
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道不应该是
window.parent
而不仅仅是parent
吗?从 iframe 内部,您应该能够执行window.parent.location = linkLocation;
编辑:
您还可以选择执行以下操作
Shouldn't it be
window.parent
rather than justparent
? From inside the iframe you should be able to dowindow.parent.location = linkLocation;
Edit:
You also have the option to do