Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the current src of an Iframe using JQuery [duplicate]

Tags:

jquery

iframe

I need to get notified whenever a user clicks a link on a page in an iframe that is not from the same domain. I'm aware of xss restrictions, however all i need to know is the current page being served in the Iframe. Is there a way to do this without violating xss rules?

like image 939
Micah Avatar asked Apr 14 '09 18:04

Micah


4 Answers

To get the iframe location using jQuery:

alert( $('iframeId').contents().get(0).location.href );
like image 176
user237119 Avatar answered Nov 10 '22 23:11

user237119


If not for cross-site scripting restrictions this should work. Unfortunately I don't know of a way to get the URL without violating these restrictions.

<html>
    <head>
        <script type="text/javascript">
            function GetIFrameUrl()
            {
                alert('url = ' + document.frames['frame1'].location.href);
            }
        </script>
    </head>
    <body>
        <a href="#" onclick="GetIFrameUrl();">Find the iFrame URL</a>
        <iframe name="frame1" src="http://www.google.com" width="100%" height="400"></iframe>
    </body>
</html>
like image 29
joshuapoehls Avatar answered Nov 10 '22 22:11

joshuapoehls


You can easily do it with vanilla js.

  • Query the document for an element with the specific name, id, class or whatever
  • Get the source for attribute for that element

     //Get by Id
     var frame_src = document.getElementById('myIframe').src
     //Get by tag name - get the first
     var frame_src = document.getElementsByName('frmExternal')[0].src
     //Alert 
     alert(frame_src)
    
like image 5
raam86 Avatar answered Nov 10 '22 21:11

raam86


  <script type="text/javascript"> // if window open in iframe then redirect to main site
         if(window.top.location != window.self.location)
         {
            alert(window.self.location);
            // top.window.location.href = window.self.location;
         }
    </script>
like image 1
Sandeep Sherpur Avatar answered Nov 10 '22 21:11

Sandeep Sherpur