Click to See Complete Forum and Search --> : Using .js files - calling
bigal
09-02-2003, 06:56 AM
If you look at my post "Cookies - Recalling (found Here (http://forums.webdeveloper.com/showthread.php?s=&threadid=16569) ) What i need is the script for remembering a name (Attached - but also in my othere topic) To be in a .js file. Ok, done that, but how do i call that file when i need it????
Example
Dear <Name>
blabalblablablabla .......<Name> blablabla <NAME>
Ect ect ect.
If the script is stored in a .js file, how do i call it in the middle of a sentance? :confused:
Please help!
DOSPrompt
09-02-2003, 07:43 AM
Once you've called a file, you don't really have to call it again, assuming all you've done is defined some functions and stuff within it... as per your example file.
So if you include that file once at the top of your document...
<script type="text/javascript" src="/root/to/script.js"></script>
...then you can use the functions defined within whenever you want after that.
So, for example, if you'd set a cookie called "Name", containing the user's name, then you could do something like...
<html>
<head>
<script type="text/javascript" src="/root/to/script.js"></script>
<script type="text/javascript">
var name = getCookie('Name');
</script>
</head>
<body>
<p>Dear <script>document.write(name);</script>,</p>
<p>Blah blah ...</p>
</body>
</html>
Hope this helps...
Oh, and btw, if you're using external JavaScript files, you don't need to include <script></script> tags in them.
bigal
09-02-2003, 09:28 AM
So what your saying is put:
<script type="text/javascript" src="/root/to/script.js"></script>
at the top of my page i want there name to be in, so its loaded with the page (pathe edited)
And where i want it to come into play,:
<html>
<head>
<script type="text/javascript" src="/root/to/script.js"></script>
<script type="text/javascript">
var name = getCookie('Name');
</script>
</head>
<body>
<p>Dear <script>document.write(name);</script>,</p>
<p>Blah blah ...</p>
</body>
</html>
I will have a look and play! ;)
bigal
09-02-2003, 09:49 AM
After a while of trying, heres what i get:
I put:
<script type="text/javascript" src="name.js"></script>
It the top of my document
I also put :
<script type="text/javascript" src="name.js"></script>
<script type="text/javascript">
var name = getCookie('Name');
</script>
In the head of my document
and
<p>Hello<script>document.write(name);</script>,</p>
Where i want the name to appear
I get : Hello Undifined!
And a error signal in the lower left :(
I have included a zip file with name.js and the html page with it currently in ( not needing name.js, just with the script in it).
If you can get a script in which i only have to put :
<script>document.write(name);</script>
where i want it to say the persons name, as this script should do, then GOOD ON YOU!!!
:cool:
bigal
09-02-2003, 02:39 PM
Please, does anyone know how to solve this prob, i could do with this to spice up my website a bit! :)
What is the error you get when you load the page? Make sure that name.js is the exact filename and it is in the same exact folder. If your function worked when it wasn't in the .js file, it should work fine while in the .js file.
[J]ona
I've found your problem, and the attached .zip file contains the solution. Remember that in .js files, you do not need the <script><!-- //--></script> tags, since they are in the HTML only. Only the script itself goes inside the .js files, though.
[J]ona
Oops, better attach the .zip file, I guess. :rolleyes:
[J]ona
bigal
09-03-2003, 03:17 AM
YAY! It works perfactly!
Do i gater that whenever i want "Hello <name>
I just put in :
<script type="text/javascript" src="name.js"></script>
Thats briliant THANKS :D :D :D
Well, you call the script from the HEAD tag:
<script type="text/javascript" src="name.js"></script>
But when you want to display the name in your BODY tag, you use something like this:
<p>Dear <script type="text/javascript"><!--
document.write(name); //--></script>,</p>
[J]ona
bigal
09-05-2003, 06:55 AM
Thanks, problem solved :D
Originally posted by bigal
Thanks, problem solved :D
You're welcome! :)
[J]ona\