body.onload += "XX"

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Feb 2005
Posts: 4
Reputation: NimbusSoftware is an unknown quantity at this point 
Solved Threads: 0
NimbusSoftware NimbusSoftware is offline Offline
Newbie Poster

body.onload += "XX"

 
0
  #1
Mar 15th, 2005
I'm trying to append a function to the body tag's onload event...

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <body onload="Do_This();"...>
  2. ...
  3. ...
  4. <script....>
  5. document.body.onload =+ " And_This();";
  6. alert(document.body.onload);
  7. </script>

The alert reports "undefinedDo_This();". The Do_This() function does load (and run) but I want both funcions to run on the onload event. The body tag is in a php "header" file that is included in the file the holds the script.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: body.onload += "XX"

 
0
  #2
Mar 15th, 2005
You can either define a third function, which would have the sole purpose of calling the other two functions.

Or, you can comma separate the two functions in your onload attribute.

Or, you can concantenate them with the "&&" operator:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <body onload="return (Do_This() && Do_That())" >

(which is a useful construct if the functions return booleans).

Lastly, you can just include the "And_This()" function in a separate script body at the very end of your document. It will run "inline" when the browser encounters it.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 182
Reputation: alpha_foobar is an unknown quantity at this point 
Solved Threads: 3
alpha_foobar's Avatar
alpha_foobar alpha_foobar is offline Offline
Junior Poster

Re: body.onload += "XX"

 
0
  #3
May 2nd, 2005
This page demonstrates a technique that can be used to add functions to a body onload, or similar attribute. The beauty of this is that it can be used in situations where your project does not control ownership of the header section of the page, but you want to assign onload or similar behaviours to a page, without destroying the behaviour that already exists.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <html>
  2.  
  3. <head>
  4.  
  5. <script type="text/javascript">
  6. function Do_This(){ alert("hello world"); }
  7. </script>
  8.  
  9.  
  10. <title>test</title>
  11. </head>
  12. <body onload="Do_This();" onunload="Do_This();">
  13.  
  14. <h1>hello world</h1>
  15. ...
  16. ...
  17. <script type="text/javascript">
  18. var oldFunction = null;
  19. function And_This(){
  20. if(oldFunction){ oldFunction(); }
  21. alert("tuna fish");
  22. }
  23. function closingFunction(){
  24. if(oldUnloadFunction){ oldUnloadFunction(); }
  25. alert("closing");
  26. }
  27. function loadBody(){
  28. oldFunction = window.onload;
  29.  
  30. window.onload = And_This;
  31. alert(window.onload);
  32. }
  33.  
  34. var oldUnloadFunction = null;
  35. function unloadBody(){
  36. oldUnloadFunction = window.onunload;
  37.  
  38. window.onunload = closingFunction;
  39. alert(window.onunload);
  40. }
  41.  
  42. loadBody();
  43. unloadBody();
  44. </script>
  45. </body>
  46. </html>

What happens here is the loadBody and unloadBody functions are called inline when the page is being drawn. At this stage the body.onload attribute has been set (you access this using the window.onload attribute). If you print this attribute, you will notice that it is a function, with the initially defined onload function forming the body of the function. This way the javascript engine preserves the arguments that have been provided.

The loadBody function then stores the current function in a global pointer variable. That can then be called as a function in the newly defined onload function.

This has been tested in Internet Explorer 6.0.2800.1106 and Mozilla Firefox 1.0.3 with build string: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC