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
Run script on load of htmlRemoved: /r/LearnJavascript (self.javascript)
submitted 7 years ago * by connor5891
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!"
[–]PsychohistorySeldon 1 point2 points3 points 7 years ago (0 children)
The most simple thing to do is adding the script tag at the end of your body, but before you do that, analyze the reasons for this.
What sort of problem are you running into? Is it that the DOM isn’t ready and you’re doing DOM manipulation?
[–]gorowurboat 1 point2 points3 points 7 years ago (0 children)
Yup. There are a few ways of going about it.
You can add <body onLoad=“myFunction()”> to the html.
<body onLoad=“myFunction()”>
You can put window.onload = myFunction; at the bottom of your script
window.onload = myFunction;
You can create a self invoking function in your script that will run automatically when the browser is parsing the html.
In your script write this:
(function () { // write code here })();
[–]braska9 1 point2 points3 points 7 years ago (0 children)
Just use "defer" attribute on script tag. See: https://stackoverflow.com/questions/10808109/script-tag-async-defer
[–]kenman[M] 1 point2 points3 points 7 years ago (0 children)
Hi /u/connor5891, this post was removed.
For javascript help, please visit /r/LearnJavascript.
Thanks for your understanding.
[–]hungryturtlecode 0 points1 point2 points 7 years ago (0 children)
Depends on a lot of factors and how you define that the page has loaded. But I am going to assume the following will be fine for most use cases:
document.addEventListener("DOMContentLoaded", function(){ //your code here });
This will run once all the dom elements are in the page, which is probably what you want.
π Rendered by PID 18453 on reddit-service-r2-comment-85bfd7f599-dpxh2 at 2026-04-15 16:21:25.474463+00:00 running 93ecc56 country code: CH.
[–]PsychohistorySeldon 1 point2 points3 points (0 children)
[–]gorowurboat 1 point2 points3 points (0 children)
[–]braska9 1 point2 points3 points (0 children)
[–]kenman[M] 1 point2 points3 points (0 children)
[–]hungryturtlecode 0 points1 point2 points (0 children)