简体   繁体   English

获取iframe的当前位置/网址并分配给变量

[英]Get current location/url of iframe and assign into a variable

I have some content hosted on my own servers which I'm displaying to the user dynamically. 我在自己的服务器上托管了一些内容,这些内容将动态显示给用户。 However the content is a few layers deep and I need the url/location of the iframe so I can assign it to a link. 但是,内容有几层深,我需要iframe的网址/位置,以便可以将其分配给链接。

The result should be when a user clicks on the link, it takes them to the page with the iframe as well as displaying the proper page/layer within the iframe. 结果应该是当用户单击链接时,它会将他们带到具有iframe的页面,并在iframe中显示正确的页面/图层。

How do I achieve this via javascript or jquery? 如何通过javascript或jquery实现此目的? Or is xpath also required for this? 还是为此需要xpath? If you disagree, please don't downvote this question but explain why I shouldn't post it since too many downvotes and you get banned. 如果您不同意,请不要对此问题进行否决,而请解释为什么我不应该发布此问题,因为过多的否决票都会被禁止。

Thanks! 谢谢!

Try to use jquery for this, like: 尝试为此使用jquery ,例如:

$(document).ready(function(){
    $("#iframeid").load(function(){
      alert(this.contentWindow.location);// do what you want with this location
    });
});

Note: First add a latest version of jquery 注意:首先添加最新版本的jquery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM