niche Posted November 23, 2011 Share Posted November 23, 2011 I want to understand this script. I can't find a reference that that says onload is a property of window. Am I thinking about this correctly? <script type="text/javascript"> window.onload=function(){ var strTesterO = document.getElementById('strTester') var str = "The cow jumped over the moon"; var spanO = document.createElement('span'); spanO.id = 'mySpan' spanO.appendChild(document.createTextNode(str)); strTesterO.appendChild(spanO); alert(document.getElementById('mySpan').clientWidth + "\n"+document.getElementById('mySpan').clientHeight); } </script> Link to comment Share on other sites More sharing options...
Ingolme Posted November 23, 2011 Share Posted November 23, 2011 The window and image object have an onload event. I don't think that, by standards, other elements have it, but all browsers support the onload event on frame elements. Link to comment Share on other sites More sharing options...
niche Posted November 23, 2011 Author Share Posted November 23, 2011 I'm still at a point in my javascript that I need to see the reference, but I haven't found one on w3s that defines window.onload. Can you send me a link? I'm bothered by not being able to find the ref on my own. Link to comment Share on other sites More sharing options...
ShadowMage Posted November 23, 2011 Share Posted November 23, 2011 W3Schools reference for the onload event is here, though it doesn't mention it belonging to the window object. Mozilla Docs has a reference for window.onload here. EDIT: Mozilla Docs also has a list of all properties and methods here. Link to comment Share on other sites More sharing options...
niche Posted November 23, 2011 Author Share Posted November 23, 2011 Makes sense. Any idea why the connection to the window object is a bit obscure? Link to comment Share on other sites More sharing options...
Ingolme Posted November 23, 2011 Share Posted November 23, 2011 Here in the event reference they show you what tags and DOM object each event can be used for:Event reference: http://www.w3schools.com/jsref/dom_obj_event.aspOnload event: http://www.w3schools.com/jsref/event_onload.asp Link to comment Share on other sites More sharing options...
niche Posted November 23, 2011 Author Share Posted November 23, 2011 Thanks. Using your last post, I changed the script, in post #1 to:(changed window.onload=function(){ to function load(){and<body> to <body onload="load()"> ) It looks good to me. Did I create a new issue in your opinion? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <title></title> <style type="text/css"> #mySpan{ display: table-cell; font-family: arial; font-weight: bold; font-size: 2em; } </style> <script type="text/javascript"> function load() { var strTesterO = document.getElementById('strTester') var str = "The cow jumped over the moon"; var spanO = document.createElement('span'); spanO.id = 'mySpan' spanO.appendChild(document.createTextNode(str)); strTesterO.appendChild(spanO); alert(document.getElementById('mySpan').clientWidth + "\n"+document.getElementById('mySpan').clientHeight); } </script> </head> <body onload="load()"> <div id="strTester"></div> </body></html> Link to comment Share on other sites More sharing options...
Ingolme Posted November 23, 2011 Share Posted November 23, 2011 I prefer not to use event attributes because they add more useless mark-up. window.onload is better than <body onload> Link to comment Share on other sites More sharing options...
niche Posted November 23, 2011 Author Share Posted November 23, 2011 Thanks again for your help and a very pleasant topic. Niche Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.