69

I have an iFrame, where I want to send a JavaScript command after loading. My current code looks like this:

<iframe src="http://www.test.tld/" onload="__doPostBack('ctl00$ctl00$bLogout','')">

But with this code the command isn't executed. What must I change to make it work? Only Chrome and Firefox have to be supported.

11
  • does iframe load test.tld page? Commented Mar 24, 2015 at 14:05
  • 1
    Yes, that works without a problem Commented Mar 24, 2015 at 14:29
  • how do you know "the command isn't executed"? Commented Mar 24, 2015 at 14:39
  • Because I am still logged in. If I click on the "Logout" button on the site which will trigger the command I am logged out. Commented Mar 24, 2015 at 14:40
  • 1
    err, you are "logged in"? try: <iframe src="http://www.w3schools.com" onload="alert('test')" /> Commented Mar 24, 2015 at 14:43

3 Answers 3

68

Use the iFrame's .onload function of JavaScript:

<iframe id="my_iframe" src="http://www.test.tld/">
    <script type="text/javascript">
        document.getElementById('my_iframe').onload = function() {
            __doPostBack('ctl00$ctl00$bLogout','');
        }
    </script>
    <!--OTHER STUFF-->
</iframe>
Sign up to request clarification or add additional context in comments.

7 Comments

maybe you can take a look at the native javascript version of this subinsb.com/jquery-to-javascript)
That also doesn't seem to work: <iframe src="test.tld" id="loadTo"> xhr.onreadystatechange=function(){ if(xhr.readyState==4 && xhr.status==200){ document.getElementById("loadTo").innerHTML = __doPostBack('ctl00$ctl00$bLogout',''); } }
i changed my code to javascript. I hope this'll work, haven't tested it myself. @Peleke
Thanks, but that also doesn't do anything. Maybe it is not possible!? The code of the logout button on the site is <a id="ctl00_ctl00_bLogout" class="logoutlink" href="javascript:__doPostBack('ctl00$ctl00$bLogout','')">(Logout)</a>
take a look at this on liveweave.com/9ngqqd i have it working here (try changing the url from the iframe to load some big pages)
|
27

document.querySelector("iframe").addEventListener( "load", function(e) {

    this.style.backgroundColor = "red";
    alert(this.nodeName);

    console.log(e.target);

} );
<iframe src="example.com" ></iframe>

2 Comments

What's the difference between this way of adding the "load" listener vs the other answer where they set iframe.onload = a function?
@rasen58, they both do exactly the same thing. The advantage of addEventListener is that you can add more than one event listener to the load event. Please refer this answer for more details. stackoverflow.com/a/6348533/9695286
11

Your code is correct. Just test to ensure it is being called like:

<script>
function doIt(){
  alert("here i am!");
  __doPostBack('ctl00$ctl00$bLogout','')
}
</script>

<iframe onload="doIt()"></iframe>

3 Comments

try: <!DOCTYPE html> <html lang="en"> <head> <script> function doIt(){ alert("here i am!"); } </script> </head> <body> <iframe onload="doIt()" src="sdsd.com"></iframe> </body> </html>
Ensure the URL being loaded into the iframe is allowed to be loaded, "check out the output in Chrome Development tools for loading erros". As if it's not allowed to be loaded the onload event will never be called. When I ran this code the URL "sdsd.com" did not have any restrictions and no loading errors. try it.
ensure the L in load is lower case, so "l" not "L" . so onload="doIt()" not onLoad="doIt()"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.