www.webdeveloper.com
Results 1 to 8 of 8

Thread: body onLoad not working

  1. #1
    Join Date
    May 2007
    Posts
    5

    body onLoad not working

    Hi All,

    I want to do some operation in the jsp page when the page is getting loaded. I tried <body onLoad="javascript:functionName()">.
    But its not working in that particular page. Althogh the same code works in other cases.
    I tried with window.onload = function(){ ... }. It worked but the page in the other frame is not getting displayed.
    Can anyone plz suggest me why
    <body onLoad="javascript:alert('hello');"> is not working ??
    or how else can I do this.

    thanks in advance.

    Regards,
    Narayan

  2. #2
    Join Date
    Nov 2002
    Posts
    4,473
    Look for javascript errors. What browser are you using?

  3. #3
    Join Date
    May 2007
    Posts
    5
    I appreciate your quick response.
    The browser is IE 6.0..
    There are no javascript errors.

    The problem is that the same code is working fine for other pages. But only in this flow it is not working.

  4. #4
    Join Date
    Jun 2007
    Location
    Cheshire, UK
    Posts
    3
    is there a URL for the page ? or if not, can you post complete code ?

  5. #5
    Join Date
    May 2007
    Posts
    5
    The file is

    <html>
    <head>
    <SCRIPT LANGUAGE="JavaScript">
    function doRedirect(){
    alert('hello');
    }
    </SCRIPT>
    </head>

    <body onLoad="javascript:doRedirect();" >
    Loading ...
    </body>
    </html>


    But, this works fine when u run it alone. But when u plug it in my environment, it wont work. So I feel something is set / not set in the previous pages which might stopping this. Is there anything like that ??

  6. #6
    Join Date
    Jun 2007
    Location
    Slovakia
    Posts
    78
    AFAIK, body has no onLoad event. There is window.onload (case matters).
    Jozef Sakalos, aka Saki
    Web Page

  7. #7
    Join Date
    May 2007
    Location
    Sweden
    Posts
    123
    You can use this:
    Code:
    <html><head>
    <SCRIPT LANGUAGE="JavaScript">
    function doRedirect(){
      alert('hello');
    }
    
    addLoadEvent(doRedirect);
    // or like this:
    addLoadEvent(function() {
    /* more code to run on page load */
    });
    
    // Multiple onload function created by: Simon Willison
    // http://simon.incutio.com/archive/2004/05/26/addLoadEvent
    function addLoadEvent(func) {
      var oldonload = window.onload;
      if (typeof window.onload != 'function') {
        window.onload = func;
      } else {
        window.onload = function() {
          if (oldonload) {
            oldonload();
          }
          func();
        }
      }
    }</SCRIPT></head><body>Loading ...</body></html>

  8. #8
    Join Date
    Sep 2006
    Location
    Cleveland
    Posts
    579
    Ont hing you need to be aware of when using an onload event or any event to that matter is timing. If you are counting on something to finish then send or do something you must always check to see if that something is done. I have run into this issue many many many... times with this app I made. The issue may not come up in some circumstances but will crop up later with out warning especially when you want to show this off to perspective clients. Been there done that like too many times. So my advice is if you use and on load event you create a function say "libinit()" which calls all the functions and does all the code for the onload part. Next is to insert this at the very bottom of the page after all the html has actually load into the browser, not necessarily to the screen but to the browser. This is because I have ran into problems where the onload event can not find the function in question but this worked perfectly for six months before that. So here is the code yo insert at the end of the page
    Code:
    <script language="javascript" type="text/javascript">
    	window.onload=function () {setCookie('secondTime','true',365);};libinit();
    </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