/    Sign up×
Community /Pin to ProfileBookmark

.js files, and the syntax to use in the html file

Hi, i’m playing around with trying to get some script off my pages and into some .js files if i’m using them on multple pages.

However, i’m experiencing two main difficulties.

1) If my .js file is to go into the header say, and it contains a function myFunction(). What’s the correct syntax to use in the html body to call that function? I thought it might be somethign like file.myFunction where file is the file.js, but i’m not having any luck with that.

2) When i use a very simple email hiding script

[code]
var myName = “someone”;
var myDomain = “host.com”;
document.write(‘<a href=”mailto:”+myName+”@”+myDomain’);
[/code]

for some reason, when i load the page, subsequent scripts don’t work. in fact none of the html really seems to work…and it’s not the <!– –> comments, because i took them all out (obviously i realised that putting //–> in a .js file isn’t going to stop anything happening in the main html body – and also, any program not handling jscript isn’t going to load the .js file anyway) – so it’s not that. Any ideas…is this a syntactical thing?

Cheers
Al

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@gokouSep 16.2003 — <html>

<head>

<title>title</title>

<script src="file.js"></script>

</head>

<body>

<script>

myFunction()

</script>

</body>

<html>

Is that what you mean?
Copy linkTweet thisAlerts:
@FangSep 16.2003 — <script type="text/javascript" src="file.js"></script>

Don't forget the users who have Javascript switched off.

2) Do not use document.write once your page has loaded.

Essentially you are writing a new page.
Copy linkTweet thisAlerts:
@theuberpuppyauthorSep 16.2003 — sorry, i'm ok with how to include external .js files. I know [i]that[/i] syntax. ?

1) it's just that it doesn't seem to be doing anything for me - in this particular instance. The code works fine when it's just written in. But, when i copy the main part (the bit in the header in this case) into a .js file, that's when it goes wrong...without me having changed anything. ?

should that not happen. Have i accidentally lost something?

Basically, I suppose what i'm asking is: does the <script src="blahblah.js"> tag actually just write the code of that file in verbatim, or is it more akin to a call - like in pure java?

2) the second problem though still remains. without changing anything at all - literally just copy and paste from working code in page file to extrnal .js file - the subsequent javascript codes still left in the main page (not related in any way at all to the code i copied - it's actually just a "datelastmodified" script still on the page) - that script doesn't even show up any more... like, what's that all about ?
Copy linkTweet thisAlerts:
@FangSep 16.2003 — 1) If it's only javascript in file.js there's no problem.

HTML must be inserted as a string variable.

2) As long as code in file.js does not interact with inline code in the wrong order(sequence of loading) all should work correctly.

Show the page(s)/link.
Copy linkTweet thisAlerts:
@theuberpuppyauthorSep 16.2003 — heehee - i just learnt a really good lesson:

namely, if you want to use a new idea, like a javascrit method you've never used before...then bleeding well try some smaller test programs first!

I understand how to implement it all now no problems...so there must and will be a problem somewhere in my code - where i've probably just put in two .'s or missed out a bracket, or something...the principle i now understand.

although... i have another question actually - but i think i'll post that separately cos it's not quite related....

it's about how to use getElementById with multiple insertions into the html document - which doesn't seem to work - probably cos id is supposed to be a unique name for a tag ?

anyway, cheers everyone for your help, bit by bit each reply helped me understand something new. Thanks

Al
×

Success!

Help @theuberpuppy 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.19,
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,
)...