";

Pass javascript variable from iframe to parent frame

So, I have a variable in my iframe, for example:

<script> var zipphone = "<?= $phone ?>"; </script> 

Now I want to pass this variable to the parent frame after loading the iframe. What is the easiest way to do this?

+6
source share
2 answers

If the pages are in the same domain, you can call the parent window function:

 window.parent.zipPhoneCallback(zipphone); 

In the parent window, you can define the following function:

 function zipPhoneCallback(zipphone) { ((console&&console.log)||alert)("zipphone = " + zipphone); } 
+10
source

Beware, it seems that there is a problem with the Chrome browser with address variables from web pages or with IFRAMES. This issue appears in offline testing.

As an aside, assuming that your browser actually implements the basic Javascript functions, you access your variable from the main page using window.myiframename.zipphone

 <IFRAME NAME="myiframename" SRC="myzipphoneiframefile.htm" .... <script type="text/javascript"> <!-- // then whatever you do with your variable // read it var z = window.myiframename.zipphone; // write to it window.myiframename.zipphone = "...."; 

etc.

Example.

DOC1.HTM Content -

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>DOC1.HTM</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor=pink> <h1>DOC1.HTM</h1> <iframe name="iframename" src="doc2.htm"></iframe> <p> <br /> <a href="#" onclick="alert('The value of the variable test_var set by a script in DOC2.HTM embedded in the DOC1.HTM page by an iframe named IFRAMENAME as read from a script running in DOC1.HTM appears to be - ' + window.iframename.test_var);return false;">check variable</a> </p> </body> </html> 

DOC2.HTM Content -

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <title>DOC2.HTM</title> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> </head> <body bgcolor=red> <script type="text/javascript"> var test_var="testing, testing . . ."; </script> <h1>DOC2.HTM</h1> </body> </html> 

This works great even with older versions of Internet Explorer, but try it while testing offline with Chrome, and window.iframename.test_var looks undefined due to a Chrome issue.

In any case, pay attention to future versions of Chrome that fix this, because Google has a lot of eggs on its face until they do.

I have a problem for this problem in offline testing of Chrome.

Thanks to Lucy24 at WebmasterWorld for the help. http://www.webmasterworld.com/google_chrome/4689258.htm#msg4689342

This problem with Chrome occurred when my javascript was tested off line , and the doc1.htm and doc2.htm files are in the same folder on my PC.

Moving the doc1.htm and doc2.htm files to the folder where I test my server-side php programs that work with Windows Internet Services Manager means that I can access the files using the http: // localhost and bingo addresses, Chrome behaves like it should have behaved offline.

In my opinion, this is not "legal" because Chrome javascript cannot directly access files in the same offline folder.

There is absolutely no security issue when running your own javascript files on your PC. Or, if Chrome wants to propose a security setting that allowed or denied an undefined change to the IFRAME variable, then OK, that will be fine.

The error message is not entirely clear. I post, and Lucy24 did everything possible to understand what this means.

If Internet Explorer and Firefox, etc. allow you to test your javascript offline, why not Chrome?

So well made Lucy24. Chrome didn’t do very well.

+2
source

Source: https://habr.com/ru/post/889998/


All Articles