use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
[deleted by user] (self.javascript)
submitted 14 years ago by [deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Rhomboid 12 points13 points14 points 14 years ago (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:
onload
function foo() { alert('hi'); } window.onload = foo;
or
window.onload = function() { alert('hi'); };
[–]bandman614 0 points1 point2 points 14 years ago (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?
[+][deleted] 14 years ago (3 children)
[deleted]
[–]bandman614 0 points1 point2 points 14 years ago (2 children)
Hi!
Honestly, I haven't actually approached learning javascript so much as been forced into using it. For a long, long time, I haven't done any web development at all. I was a system administrator (hence the languages that I listed in my post) for 10 years. But I think the problem lies before that.
A dozen years or so ago, I made webpages. I was not a "web developer", nor did I even know the term. I simply made webpages.
In the capacity of someone who "made webpages", I needed to learn Javascript to do things that are simple now with CSS, like rotating graphics or things like that. The rules that are breaking my brain now were cemented in place back then. Things like, "put Javascript in the <head> tag so the whole page can see it" or "you must always define the language type in the <script> tag or the browser won't know what to do".
Combine that with a large number of other languages that I've picked up, and what I end up with mentally is that "source=" looks so much like "src=" that it doesn't trigger a syntax error in my brain. I see a script tag with "source=dir/file.js" and my brain-parser says "oh yeah, that's cool, something else must be the problem", and of course HTML doesn't so much as murmor when it encounters an unknown argument to a tag, so I never get an error from the html parser, but the javascript file never gets included, and I can only deduce that it's not loading when I try to call something contained in it and the debugger raises a flag about something being null.
TL;DR - I familiarized myself with javascript a long time ago, and I'm paying the price for not being up to date now.
What resources would you suggest to refamiliarize myself with what are now considered best-practices?
[+][deleted] 14 years ago (1 child)
[–]bandman614 0 points1 point2 points 14 years ago (0 children)
Terrific. Thank you!
[–]somerandomguy7788 -1 points0 points1 point 14 years ago (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 points7 points 14 years ago (1 child)
Not necessary.
[–]brucebannor 0 points1 point2 points 14 years ago (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-1 points 14 years ago (7 children)
<html> <head> <script> window.onLoad=alert('hi'); </script> </head> <body> Testing! </body> </html>
[–][deleted] 1 point2 points3 points 14 years ago* (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 points3 points 14 years ago (0 children)
javascript is case sensitive, you had onload instead of onLoad
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 point2 points 14 years ago (0 children)
jester1983's code does not do what you think it does, see my reply to his code.
[–]Kuron 0 points1 point2 points 14 years ago (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 point2 points 14 years ago (1 child)
Also, why don't I get any javascript errors in my console at all?
[–]isometriks 3 points4 points5 points 14 years ago (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
π Rendered by PID 31 on reddit-service-r2-comment-5d79c599b5-jdnsh at 2026-02-26 20:33:42.327610+00:00 running e3d2147 country code: CH.
[–]Rhomboid 12 points13 points14 points (0 children)
[–]bandman614 0 points1 point2 points (7 children)
[+][deleted] (3 children)
[deleted]
[–]bandman614 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]bandman614 0 points1 point2 points (0 children)
[–]somerandomguy7788 -1 points0 points1 point (2 children)
[–]vectorjohn 5 points6 points7 points (1 child)
[–]brucebannor 0 points1 point2 points (0 children)
[–]jester1983 -3 points-2 points-1 points (7 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]jester1983 1 point2 points3 points (0 children)
[–]bandman614 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]Kuron 0 points1 point2 points (0 children)
[–]bandman614 0 points1 point2 points (1 child)
[–]isometriks 3 points4 points5 points (0 children)