SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2005
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to fire javascript functions during load and unload page?

    i discovered this problem because, i am using <frameset></frameset> tag at the index.html. what i want to do is, to fire greeting functions for onload and unload page. anyone can help me?

    Code:
    <html>
    
    <head>
    
    <title>My Personal Website</title>
    
    <script language = "javascript">
    function welcomeVisitor(){
    	alert("Welcome to my personal website.");
    }
    
    function byeVisitor(){
    	alert("Thank you for your time. Please come back again.");
    }
    </script>
    
    </head>
    
    <frameset border="0" rows="180,*">
    	<frame src="_main/top.html">	
    <frameset border="0" cols="180,*">	
    	<frame src="_main/menu.html">
    	<frame src="_main/intro.html" name="content">
    </frameset>
    </frameset><noframes></noframes>
    
    <body onLoad = "welcomeVisitor()" onunload = "byeVisitor()">
    
    
    </body>
    
    </html>

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,187
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    frames and body tag

    First, take out the body tags. To call a function you can put the events in the script like this
    HTML Code:
    <script language = "javascript">
    function welcomeVisitor(){
    	alert("Welcome to my personal website.");
    }
    
    function byeVisitor(){
    	alert("Thank you for your time. Please come back again.");
    }
    window.onload = welcomeVisitor;
    window.onunload = byeVisitor;
    </script>

  3. #3
    padawan silver trophybronze trophy markbrown4's Avatar
    Join Date
    Jul 2006
    Location
    Victoria, Australia
    Posts
    4,108
    Mentioned
    28 Post(s)
    Tagged
    2 Thread(s)
    window.onload = welcomeVisitor;

    The more modern approach to hooking events to a function is to use addEventListener(event, function, false);

    So in the example it would be:

    window.addEventListener('load', 'welcomeVisitor', false)..

    A better way to do it if there are more than the one function you wish to run on load is to include them all in an addEventListeners function..

    function addEventListeners() {

    welcomeVisitor();
    // Some more functions that are run on load

    }

    Then set up the event listeners function to be run on load

    window.addEventListener('load', 'addEventListeners', false);


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
  •