0

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.

  • 2
    check out: http://stackoverflow.com/questions/3420004/access-parent-url-from-iframe – Alex Yurkowski Oct 06 '15 at 00:17
  • If you want to get the main window URL you shouldn't try to get the URL in iframe.html. window.location.href will always get the URL of the CURRENT FILE. So if you want to get the full URL, you will have no choice but to alert the URL in test.html instead. – jessica Oct 06 '15 at 00:21

1 Answers1

1

Try utilizing document.referrer

<iframe src="data:text/html,<!DOCTYPE html><html><body><script>document.write(document.referrer)</script></body></html>">
</iframe>
guest271314
  • 1
  • 10
  • 82
  • 156
  • thanks for your answer. but i cannot touch the main html page as it is coming from third party. i can use only iframe.html page and jquery inside that page. pls help me how can i solve this – Vinay Reddy Oct 06 '15 at 15:57
  • @VinayReddy _"cannot touch the main html page as it is coming from third party. i can use only iframe.html page and jquery inside that page."_ Yes, tried `document.referrer` at `iframe.html` to retrieve parent `window` url ? – guest271314 Oct 07 '15 at 01:00
  • Thankyou, You r right. Even I think the same. But just I am trying if there is any possible way – Vinay Reddy Oct 07 '15 at 01:56