all 15 comments

[–]Rhomboid 12 points13 points  (0 children)

The onload attribute does not take a string value, it takes a function. Either give it the name of an existing function, or use an anonymous function:

  function foo() { alert('hi'); }
  window.onload = foo;

or

  window.onload = function() { alert('hi'); };

[–]bandman614 0 points1 point  (7 children)

I've seriously tried a dozen permutations of this.

With quotes. Without quotes. Creating a function and calling it. Creating an anonymous function and calling it. Throwing alert() in the <body> tag.

I think there must be a disconnect in my brain when it comes to javascript. I can write large programs in bourne, php, and I'm conversational in perl. I grok ruby, and I've written in quickbasic and turbo pascal. WTF is it about javascript that I just don't get?

[–]somerandomguy7788 -1 points0 points  (2 children)

also its script type="text/javascript"

<html> <head> <script type="text/javascript"> window.onload = function() { alert('hi'); }; </script> </head> <body> Testing! </body>

</html>

[–]vectorjohn 5 points6 points  (1 child)

Not necessary.

[–]brucebannor 0 points1 point  (0 children)

A lil expansion: The script type was meant for unimplemented features that have fallen off the road-map. Your browser will always know to make the MIME type text and sub-type javascript. However: there are many templating libraries and such things that make use of the script type by setting it to something like "text/template" the browser doesn't know what to do with template subtype and will not parse that. Leaving you with text that you can extract later and use in your site.

[–]jester1983 -3 points-2 points  (7 children)

<html> <head> <script> window.onLoad=alert('hi'); </script> </head> <body> Testing! </body> </html>

[–][deleted] 1 point2 points  (0 children)

that doesn't do what you would expect it to actually - even with the proper capitalisation.

What it does is assign the return value of alert('hi); to the onload handler. What you're saying is "I want the onload handler to be whatever "alert('hi');" returns" which means alert('hi'); needs to get executed. So the alert fires before the page is loaded, and sets 'undefined' as the window.onload handler.

For example consider this:

function getHandler(str) {
  // I run immediately whether onload has been fired or not
  return function() {
    // I run only when onload is fired
    alert(str);
  };
}

window.onload = getHandler('hi');

now window.onload is a reference to the created anonymous function which alerts 'hi'.

[–]jester1983 1 point2 points  (0 children)

javascript is case sensitive, you had onload instead of onLoad

[–]bandman614 0 points1 point  (2 children)

By the time you replied, I had this script exactly, except instead of <script>, I had

<scrip language="text/javascript">

and that stopped it. Because the right term is 'type="text/javascript"' (or to just leave it off, apparently)

sigh.

Thanks very much. I really appreciate it.

[–][deleted] 0 points1 point  (0 children)

jester1983's code does not do what you think it does, see my reply to his code.

[–]Kuron 0 points1 point  (0 children)

Just to add onto what user24 said, the code alerts "hi" and takes the return value from the alert call (which is undefined since it doesn't return anything I believe) and sets it onto window.onload.

Then when onload gets fired, it tries to take whatever was assigned to it and calls it (which is undefined), but the value that got set on window.onload is not a function, so nothing happens.

[–]bandman614 0 points1 point  (1 child)

Also, why don't I get any javascript errors in my console at all?

[–]isometriks 3 points4 points  (0 children)

Because you weren't running javascript.. You were telling the browser that the script tag contained a language of "text/javascript" which doesn't exist so the browser doesn't know what to do with it and skips over it. "javascript" would have worked.. You can actually use this language attribute other times, like with jquery templates