javascriptjqueryurldomiframe

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.

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.

How do I achieve this via JavaScript or jQuery? Or is XPath also required for this?


Solution

  • Try to use jquery for this, like:

    $(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