简体   繁体   中英

calling javascript from js file

I have elements on my parent page that changes only if hidden iframe within contain one div id.

Everything is fine if I let code on html page :

 <iframe id="myiframe" style="display:none" onload="load()" name="myiframe"   src="mysite.com"></iframe>

JavaScript:

function load(){


if (window.frames[0].document.getElementById('applications'))
{
var str=document.getElementById("tst").innerHTML;
var n=str.replace("Login","Logout");}}
document.getElementById('tst').href='logout';

But I need iframe code deleted from html file and relocated js file.

I use this code in js file to trigger iframe:

window.onload = function(){
var link = "iframelink"
 var iframe = document.createElement('iframe');
iframe.frameBorder=0;
iframe.width="300px";
iframe.height="250px";
iframe.id="randomid";
iframe.addEventListener("load", load);
iframe.setAttribute("src", link);
document.getElementById("ad54").appendChild(iframe);
}

html code to get iframe:

<div id="ad54"</div>

Iframe launches, but right now I get no elements changed on parent page although iframe is triggered . When iframe is loaded I need those changes to take place in parent page.

<div> elements do not load external content, so they never fire load events.

You appear to be trying to fire the event when the iframe content loads. You need to attach your event listener to the iframe.

iframe.addEventListener("load", load);
iframe.setAttribute("src", link);

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