Page 2 of 2 FirstFirst 12
Results 11 to 20 of 20

Thread: Iframe onload event only after an anchor onclick

  1. #11
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    47
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    Another option of course, is to trigger the listen() function when you click the button. Whichever you prefer, but the way I just mentioned means that the function only runs when you click the button.

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function listen() {
    	var iframe = document.getElementById("iframe_a");
    	var onload = iframe.onload;
    	if (typeof iframe.onload != 'function') {
     		iframe.onload = alert_change;
    	} else {
     	   	window.onload = function() {
         	  		onload();
          	   		alert_change();
    		}
    	}    
    };   
    function alert_change() {
       alert("thanks for visiting!");
    }
    </script>
    </head>
    <body>
    <iframe name="iframe_a" id="iframe_a"></iframe>
    <a href="http://www.example.com/" target="iframe_a" onclick="listen()">Go!</a>
    </body>
    </html>
    Last edited by bernie1227; 10-14-2012 at 07:45 AM. Reason: detail
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  2. #12
    Join Date
    Nov 2009
    Posts
    211
    Thanks
    40
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by keyboard1333 View Post
    Here's bernie's code combined with yours
    That doesn't work in IE8, either.

    jscheuer's code should work just fine.
    John is a super coder and his codes always work although I was expecting something simpler.
    Thanks again!

  3. #13
    Join Date
    May 2012
    Location
    Hitchhiking the Galaxy
    Posts
    1,013
    Thanks
    47
    Thanked 139 Times in 139 Posts
    Blog Entries
    1

    Default

    As I said before:
    If this thread is finished, please set it to resolved.
    You can do this by editing the first post within the thread - Pressing go advanced - Then where it says no prefix, selecting resolved then save.
    "Most good programmers do programming not because they expect to get paid or get adulation by the public, but because it is fun to program." - Linus Torvalds
    Anime Views Forums
    Bernie

  4. #14
    Join Date
    Nov 2009
    Posts
    211
    Thanks
    40
    Thanked 1 Time in 1 Post

    Default

    Thank you so much, John!

  5. #15
    Join Date
    Nov 2009
    Posts
    211
    Thanks
    40
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by bernie1227 View Post
    As I said before:
    If this thread is finished, please set it to resolved.
    You can do this by editing the first post within the thread - Pressing go advanced - Then where it says no prefix, selecting resolved then save.
    Done!

  6. #16
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    You're welcome! I was playing around a bit more. Here's ar version that only alerts the first time:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    function on_iframe_load(way){
    	var frame = document.getElementById('iframe_a');
    	if (window.addEventListener){
    		way === 'remove' && frame.removeEventListener('load', on_iframe_load.loadfunc, false);
    		way === 'add' && frame.addEventListener('load', on_iframe_load.loadfunc, false);
    	}
    	else if (window.attachEvent){
    		way === 'remove' && frame.detachEvent('onload', on_iframe_load.loadfunc);
    		way === 'add' && frame.attachEvent('onload', on_iframe_load.loadfunc);
    	}
    }
    on_iframe_load.loadfunc = function() {
    	on_iframe_load('remove');
    	alert('Thanks for the visit!');
    };
    </script>
    </head>
    <body>
    <iframe name="iframe_a" id="iframe_a"></iframe>
    <a href="http://www.dynamicdrive.com/" target="iframe_a" 
    onclick="on_iframe_load('add'); this.onclick = function(){return true;};"
    >Go!</a>
    </body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. #17
    Join Date
    Nov 2009
    Posts
    211
    Thanks
    40
    Thanked 1 Time in 1 Post

    Default

    I really appreciate everyone's help on this thread!
    I came up with a solution and would be happy to know what you think:

    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <iframe id="iframe_a" name="iframe_a" onload="if(this.className=='active'){alert('Thanks for the visit!');};"></iframe>
    <a href="http://www.example.com/" target="iframe_a" onclick="document.getElementById('iframe_a').className='active';">Go!</a>
    </body>
    </html>

  8. #18
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,524
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    It works fine... personally, I'd go with John's solution just to avoid cluttering up your html with inline js. (or even do what you did inside a function)
    P.s. If your still looking for more responses, I'd suggest changing the thread fromm resolved... Only set it to resolved when you don't want people to post anymore.

    Edit:
    If you only want it to fire once -
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title></title>
    </head>
    <body>
    <iframe id="iframe_a" name="iframe_a" onload="if(this.className=='active'){alert('Thanks for the visit!');this.className='inactive';};"></iframe>
    <a href="http://www.example.com/" target="iframe_a" onclick="document.getElementById('iframe_a').className='active';">Go!</a>
    </body>
    </html>
    Last edited by keyboard; 10-15-2012 at 01:21 AM.

  9. #19
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,070
    Thanks
    44
    Thanked 3,216 Times in 3,178 Posts
    Blog Entries
    12

    Default

    I'm a big fan of 'whatever works' as long as it's truly cross browser and truly accomplishes your aim.

    With this latest from Rain Lover we would probably see (as we would with most of the workable solutions offered so far) every load of the iframe after page load firing the alert. That includes navigation within the iframe, as well as clicking on the link a second, third, etc. time(s). I guess what I'm trying to say, rather ask, is, "What exactly do you want to accomplish, and why?"
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #20
    Join Date
    Nov 2009
    Posts
    211
    Thanks
    40
    Thanked 1 Time in 1 Post

    Default

    Quote Originally Posted by jscheuer1 View Post
    I guess what I'm trying to say, rather ask, is, "What exactly do you want to accomplish, and why?"
    The fact is I was going to customize a Google Docs form when I came across the following problem:
    Redirect a Google Docs form to your custom 'thank you' page

    There was no more answers and I wasn't satisfied with the only solution offered. So I tried to paraphrase my question with a similar problem. But now I guess(?) bernie's solution is good.

Similar Threads

  1. Modal onload event?
    By mdh in forum Dynamic Drive scripts help
    Replies: 4
    Last Post: 11-29-2009, 06:21 PM
  2. Onclick event -> onload event
    By Goodboy in forum JavaScript
    Replies: 0
    Last Post: 08-31-2009, 01:30 PM
  3. Jump to anchor tag onload
    By bigalo in forum JavaScript
    Replies: 2
    Last Post: 08-14-2009, 02:11 PM
  4. Use different tag for onload event
    By mandarin in forum Dynamic Drive scripts help
    Replies: 0
    Last Post: 05-06-2009, 04:40 AM
  5. Onload versus onclick event handler
    By CzechBoy in forum Dynamic Drive scripts help
    Replies: 5
    Last Post: 02-28-2007, 10:44 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •