/    Sign up×
Community /Pin to ProfileBookmark

Moving the source code to the JS file

Hi
As I am adding more and more features to the webpage…. i end up using more and more JS code… and to keep it separate for the HTML code… I want to move the JS code to a separate file…. but I access some elements using ‘document.getElementById’ method… which I have noticed is not accessible from the JS file… and also I am passing an object as an argument in some of the functions…. which i cannot access from the JS file…

Is there anyway, i can move such functions to the JS file? Please help me…

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@so_is_thisNov 24.2006 — ...I access some elements using 'document.getElementById' method... which I have noticed is not accessible from the JS file... and also I am passing an object as an argument in some of the functions.... which i cannot access from the JS file... [/QUOTE]
Neither of these statements are true. Accessing the DOM from a [B].js[/B] file and passing an object reference to a function in a [B].js[/B] file are both entirely normal things to do. You see, a [B].js[/B] file is included by the browser just as if it had always been a part of the HTML source from the very start. Using a [B].js[/B] file is completely seemless.
Copy linkTweet thisAlerts:
@GotMex_Nov 24.2006 — Could it be that you are trying to access elements on your page via the document.getElementById method before the page has loaded? When you put your JS in a file, all of it runs BEFORE the document is rendered on screen, so JavaScript can't find any elements because they don't exist yet.

You solve this by writing all of your code inside of functions, which you then call with the onLoad event so that they only fire once the whole document has been loaded.
Copy linkTweet thisAlerts:
@so_is_thisNov 24.2006 — ...or, you just include the [B].js[/B] file at the [I]*[U]end[/U]*[/I] of the document body.

There are no real restrictions which prevent you from doing this. ?
Copy linkTweet thisAlerts:
@GotMex_Nov 24.2006 — Isn't it just better style to include scripts in the head section, and run JS code once you are sure the document has loaded?
Copy linkTweet thisAlerts:
@so_is_thisNov 24.2006 — "Better" style? I don't think so. Flexibility to the needs of a situation is the "better" style (IMHO). Of course, you have those pundits which think all script (including in-line events) should be removed from the HTML content. But there are too many dictators in the world already. The only reason I've ever heard given for separating the script from the HTML content is because of HTML readers for the blind. I say that if the readers are having trouble, then somebody better design better readers. If the browser can figure out how to parse the content, then so can the readers. Somebody needs to take responsibility for themselves -- rather than blaming everybody else for their problems.
×

Success!

Help @Gameone spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...