www.webdeveloper.com
Results 1 to 8 of 8

Thread: Need help understanding window.onload

  1. #1
    Join Date
    Feb 2012
    Posts
    4

    Need help understanding window.onload

    I am currently trying to figure out a way to create a dialog box for visitors that come to my blog.
    This is the Code i have currently
    <script type="text/javascript">
    window.onload= loaded;
    function loaded()
    {
    alert('YOUR MESSAGE HERE');
    }
    </script>
    The problem is that the dialog box appears every time some one changes to the next page. Does any one mind helping me out? Please it would be very much appreciated.

  2. #2
    Join Date
    Feb 2012
    Posts
    218
    Use cookies.
    Code:
    <script type="text/javascript">
     window.onload= loaded;
     function loaded()
     {
        if(!getCookie('returning')){
            alert('YOUR MESSAGE HERE');
            setCookie('returning',1,999)
        }
     }
     </script>
    Use cookie functions from http://www.w3schools.com/js/js_cookies.asp

  3. #3
    Join Date
    Feb 2012
    Posts
    4

    Thanks

    I sort of want the message to pop up every time they enter the my sites domain. Does any one mind helping me figure out how to set the cookies and have them expire once they leave the domain?
    I'm sorry if I'm asking really stupid questions, I'm very much inexperienced with coding in general.

  4. #4
    Join Date
    Feb 2012
    Posts
    218
    Then you can use the same code, but put
    setCookie('returning',1)
    This way the cookie reset everytime the user closes the browser.
    Next time the user opens the browser and enters your site, the alert box will apear.

  5. #5
    Join Date
    Feb 2012
    Posts
    4
    Thanks alot man for the help, I did what you said and removed the 999. But for some reason ever since I put the cookie function in the Dialog Box has not poped up, at all.

    Heres the code i have right now T.T, This is so confusing....
    Code:
    <script type="text/javascript">
     window.onload= loaded;
     function loaded()
     {
        if(!getCookie('returning')){
            alert('YOUR MESSAGE HERE');
            setCookie('returning',1)
        }
     }
     </script>

  6. #6
    Join Date
    Feb 2012
    Posts
    218
    Did you include the cookie functions from this page?
    http://www.w3schools.com/js/js_cookies.asp

  7. #7
    Join Date
    Feb 2012
    Posts
    4
    T.T ooft. Thanks man haha im so stupid, It works perfectly now Thanks alot.

    Would you know if there was some sort of timer system I would be able to put this on? In order for it to pop up after like 10 seconds or so?

  8. #8
    Join Date
    Feb 2012
    Posts
    218
    Code:
    <script type="text/javascript">
     window.onload= function(){
        window.setTimeout('loaded()', 10000)
     }
    
     function loaded()
     {
        if(!getCookie('returning')){
            alert('YOUR MESSAGE HERE');
            setCookie('returning',1)
        }
     }
    </script>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles