Vinay Reddy
Vinay Reddy

Reputation: 63

jQuery iframe- how to get parent window URL when using an iframe?

I have given an example.

test.html:

<!DOCTYPE html>
<html>
 <head>
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js">
 </head>
 <body>
   <h1>main window test</h1>
   <iframe src="./iframe.html"></iframe>
 </body>
</html>

iframe.html:

 <!DOCTYPE html>
   <html>
     <head>
       <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
     </head>
     <body>
         <h1>iframe testing</h1>
         <script type="text/javascript">
             $(document).ready(function() {
                 alert("window.location.href"+window.location.href);
             });
         </script>
      </body>
    </html>

In alert--> i am getting the iframe url "C:\Users\nn96589\Desktop\testfolder\iframe.html"

but, i need the main window URL which is "C:\Users\nc96589\Desktop\testfolder\test.html"

can anyone help me. Thanks in advance.

Upvotes: 0

Views: 1646

Answers (1)

guest271314
guest271314

Reputation: 1

Try utilizing document.referrer

<iframe src="data:text/html,<!DOCTYPE html><html><body><script>document.write(document.referrer)</script></body></html>">
</iframe>

Upvotes: 1

Related Questions