Click to See Complete Forum and Search --> : .js files, and the syntax to use in the html file


theuberpuppy
09-15-2003, 11:50 PM
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


var myName = "someone";
var myDomain = "host.com";
document.write('<a href="mailto:"+myName+"@"+myDomain');


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

gokou
09-16-2003, 01:29 AM
<html>
<head>
<title>title</title>
<script src="file.js"></script>
</head>
<body>
<script>
myFunction()
</script>
</body>
<html>

Is that what you mean?

Fang
09-16-2003, 02:14 AM
<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.

theuberpuppy
09-16-2003, 03:31 AM
sorry, i'm ok with how to include external .js files. I know that 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 :confused:

Fang
09-16-2003, 05:32 AM
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.

theuberpuppy
09-16-2003, 12:44 PM
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 :D

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

Al