/    Sign up×
Community /Pin to ProfileBookmark

Am I doing this right!!?

Hi all

I am including a js file in the HEAD of my page as such:

<script type=”text/javascript” src=”/scripts/common.js”></script>

and just for test purposes, in my common.js file I have the following:

[CODE]
function tester() {
alert(‘tester’);
}

window.onload = tester();
[/CODE]

Am I right in thinking that when I view my web pages, I should get an alert? For some reason it is not alerting anything!!!

Thanks for reading.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@toicontienDec 18.2008 — Almost correct. The () on the line [B]window.onload = tester();[/B] causes the tester function to execute, and the value returned by tester() -- in this case undefined -- is assigned to the window.onload property. You want:
[CODE]window.onload = tester; // <-- No ()[/CODE]
Copy linkTweet thisAlerts:
@JMRKERDec 18.2008 — All appears good. ?

Are you sure that the 'comon.js' file is located

in '/scripts/'

and not './scripts/'

(note the './' relative directirve)?

This assumes that your 'scripts' directory is NOT a subdirectory of your root directory

but is a subdirectory of where your program is located.

?

:o

Or NOT, according to 'toicontien' in post I didn't see.

(Probably a better answer). :eek:
×

Success!

Help @kbc1 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.16,
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,
)...