add javascript function to parent

Go To


I want to add from iframe window a custom javascript function to the main window .

if i append to body of main page

<script type='text/javascript'><!--//<![CDATA[
   var ox_u = '';
   if (document.context) ox_u += '&context=' + escape(document.context);
   document.write("<scr"+"ipt type='text/javascript' src='" + ox_u + "'></scr"+"ipt>");

would it work?

here my first try, but seems no luck... I thhink I am escaping wrong way...

Could you hint out, pleaseee...

Paste from JSFIDDLE

Actually This is based on another code. And Jquery is not a must. I only need to paste the given ad code of OPEN X from within IFRAME to the parent's directory in some specific div by ID... Maybe you could hint out easyer workout? Problem is that the javascript given should run in Iframe ....

2012-04-04 16:48
Why the jQuery tag - j08691 2012-05-05 22:28


I assume you are saying that you want to access the parent window from within the iframe window. To do this, all you need to do is:

a. Make sure the parent window has the same "document.domain" as the child iframe, for example ""

b. You can dynamically add a variable to the parent document by simple doing: = "yay";

In the above example, "myNewVariable" would be accessible from the parent document.

c. If you want to include an external script into the parent document, go ahead and create the script tag and append it to the parent document.

    var myNewScript = document.createElement('script');
    myNewScript.src = "SOURCE OF SCRIPT";"body")[0].appendChild(myNewScript);

Let me know if that works for you!

2012-05-05 22:24
by Osmaan Shah