PDA

View Full Version : Help Required with Bookmark Site Script



Jeremy I
11-13-2005, 06:37 PM
Hi,

I am quite new to Javascript and I usually copy and paste.

I have seen your excellent Bookmark Site Script which works in Firefox & IE
and I would like to know if it is possible to save the "Head" part of the script in an external .js file?

If it is possible to do this, what code change is required for the javascipt command in the Body Section?

Many thanks

Jeremy I

jscheuer1
11-13-2005, 07:09 PM
If done correctly, the part in the body remains unchanged, here are my general guidelines for making a script external:


Use a text editor to save the script, call it 'file_name.js' where 'file_name' can be any valid file name of your choosing. Substitute the name of your external .js file for some.js in the below:


<script src="some.js" type="text/javascript"></script>

Common problems arise when:

1 ) The script file is not in the directory specified. In this example it must be in the same directory as the page(s) that use it.

2 ) Opening, closing and/or 'hiding' tags are left in the external file. This means that you must strip:
<script>
<!--and
//-->
</script>and any of their many variations from the beginning and end of the external file.

3 ) The external call (<script src="some.js" type="text/javascript"></script>) is not inserted into the page at the correct spot. The external call must be inserted at the same place on the page where the script was/would have been.

4 ) Paths to other files (if) used by the script are no longer valid due to its location. This is only a problem if the external script is kept in a different directory than the page it was working on when it was an internal script. To correct this, use absolute paths inside the script.

5 ) Inappropriately combining two or more scripts into one external file. Usually external scripts can be combined if one knows enough about scripting to do so properly. Even then it is possible to overlook something.

A rule of thumb when testing is, if it won't work on the page, it won't work as an external file either.

Jeremy I
11-14-2005, 12:18 AM
Hi John,

Thanks for the advice. In the past I have created external scripts and saved them correctly in other locations. I have complied mostly with your suggestions.

I can get the script to work by making the Script Internal.

I have an external Javascript folder in the site and I think that the only problem that I have is to try and redirect the link below to the folder.

<a href="javascript:bookmarksite('Dynamic Drive', 'http://www.dynamicdrive.com')">Bookmark this site!</a>

I have tried re-writing: <a href="javascript:bookmarksite( but I canīt seem to get it to work.

Any suggestions appreciated.

Best regards

Jeremy I

jscheuer1
11-14-2005, 05:38 AM
That part (in the body) doesn't need to be directed to any folder (it does need to be edited to reference the page you want bookmarked instead of Dynamic Drive). A call to the external script, placed in the head (where the script was when internal) does need to include the path to the external script, if different than the page's path:


<script src="script/some.js" type="text/javascript"></script>

or


<script src="../script/some.js" type="text/javascript"></script>

or whatever is the correct path on the server there.

Jeremy I
11-14-2005, 08:50 AM
Thanks John,

It works perfectly.

I appreciate your help,

Best regards,

Jeremy I