0

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.

        <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.

Edit: I know because of security policy we can't get URL when it is different domain. But I need the value from my local HTML page(the Link URL What I Clicked ). I didn't tried in this case

Any help is needed regarding this.

2 Answers 2

2

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. Something like

function iframeLinkClicked(url) {
     //do stuff
}

Then in the child window add a function to handle the click event of a hyperlink.

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.

You can add the event to the frame elements when the frame is loaded, if the frame html is in the same domain.

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. So the iframe still needs to have the load property set.

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>
Sign up to request clarification or add additional context in comments.

8 Comments

Works good, But I struck in middle. Bcz I will load many HTML files into IFrame one by one I have to add onclick to every anchor tag while loading into HTML page to IFrame & as well as function in the head tag. It looks heavy in my case. Can you suggest me any other suggestion if possible. Thanks
I updated my answer so you don't have to make any changes to the child pages. Just add the script to the parent page and it will set all the events there.
It works.. Now I know the href value. One last question is I dont want to load that external link in my Iframe. I want to disable the onLoad event when click on link.
You set set onload to null in the load function so that it only calls Load() once. You have to pass the iframe into the function by passing this as a parameter. I updated my answer to show both the iframe markup and the updated function. If this answer helped you, please mark it as the answer so others can find it. Thank you.
yes.. Surely, I learned something; But still It didn't get my requirement.With out using onload the link URL is opening in IFrame. Here I know one thing If you set attribute target as blank it will open in new window.
|
1

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
  }
}

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.