简体   繁体   English

来自IFrame Javascript的链接的网址

[英]URL of link from IFrame Javascript

This question may seen before. 这个问题可能以前见过。 But in my scenario I didn't get the success. 但是在我的情况下,我没有获得成功。 I have local HTML file loading in Iframe with sample external Link. 我在Iframe中加载了带有示例外部链接的本地HTML文件。

        <a href="http://www.boutell.com/">sample.com</a>

Code: 码:

    <iframe id="iframe_Id" src="sample.html" 
style="background-color:white; height:100%; width:100%;" onload="Load()"; ></iframe>

    function Load() {
alert("loaded");
    var linkchange = document.getElementById("iframe_Id").contentWindow.location;
alert(linkchange);
}

While click on the external link I need the url value of particular anchor tag. 在单击外部链接时,我需要特定锚标记的url值。

Edit: I know because of security policy we can't get URL when it is different domain. 编辑:我知道由于安全策略,当它是不同的域时,我们无法获取URL。 But I need the value from my local HTML page(the Link URL What I Clicked ). 但是我需要本地HTML页面中的值(“链接URL,我单击的内容”)。 I didn't tried in this case 我没有在这种情况下尝试

Any help is needed regarding this. 对此需要任何帮助。

You can call a function in the parent window when you click on a link in the frame. 单击框架中的链接时,可以在父窗口中调用函数。

In the parent html you need to add a function to handle the call. 在父html中,您需要添加一个函数来处理调用。 Something like 就像是

function iframeLinkClicked(url) {
     //do stuff
}

Then in the child window add a function to handle the click event of a hyperlink. 然后在子窗口中添加一个函数来处理超链接的click事件。

function linkClicked(hyperLink) {
    parent.iframeLinkClicked(hyperLink.href);
}

You need to add this to all hyperlinks 您需要将此添加到所有超链接

<a href="http://www.google.com" onclick="linkClicked(this);">google</a>

Now, when you click on a link, it will alert the parent window. 现在,当您单击链接时,它将警告父窗口。

Edit : alternative to adding "onclick" to every hyperlink. 编辑 :向每个超链接添加“ onclick”的替代方法。

You can add the event to the frame elements when the frame is loaded, if the frame html is in the same domain. 如果框架html位于同一域中,则可以在框架加载时将事件添加到框架元素中。

This is the complete script in the parent page. 这是父页面中的完整脚本。

<script type="text/javascript">

    function iframeLinkClicked(url) {
        alert(url);
    }
    function Load(iframe) {
        iframe.onload = null;
        try {
            var a = document.frames['iframe_Id'].document.getElementsByTagName('a');
            for (var i = 0; i < a.length; i++) {
                a[i].onclick = function () {
                    parent.iframeLinkClicked(this.href);
                    return false;
                };
            }
        } catch (e) {

        }

    }
</script>

This will replace your current Load() function. 这将替换您当前的Load()函数。 So the iframe still needs to have the load property set. 因此,iframe仍然需要设置load属性。

There is nothing to add to the child pages anymore. 子页面不再添加任何内容。 It's all done here. 都做完了

<iframe id="iframe_Id" src="sample.html" 
style="background-color:white; height:100%; width:100%;" onload="Load(this);" ></iframe>

have a look at this: 看一下这个:

var a = document.frames['iframe_Id'].document.getElementsByTagName('a');

for(i=0; i<a.length; i++) { 
  a[i].onclick = function() { 
    document.getElementById("iframe_Id").src = this.href;
    // and so on whatever you want to do
  }
}

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

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