简体   繁体   中英

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.

Try utilizing document.referrer

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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