Welcome to WebmasterWorld Guest from 54.234.153.186

Forum Moderators: brotherhood of lan & mack

Message Too Old, No Replies

Creating external javascripts

external javascripts

     

mrw770

8:05 pm on Apr 17, 2003 (gmt 0)

10+ Year Member



I found a really great free javascript to replace the menus on my website, but would like to make it into an external js file so I don't have to hard-code it onto every page anymore. I've done this for a basic drop-down script before, but this one has two parts: one for the head section and one for the body of my HTML page. Do I need to break this into two files with references in each part (if so, I must be doing it wrong) or is there some other trick to it? Or do I just need to find a simpler script and scrap this plan? If needed, I can post the script here (it's pretty long). Thanks for any help you can give!

oilman

8:12 pm on Apr 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



externalize the part the goes in the head section - that's the functional part of the code. The code in the body is what uses the funtions of the part in the head and needs to stay in the page. I'm guessing a little bit here since I don't know exactly how you're setup but I think that's usually the case.

oh - and welcome to WebmasterWorld :)

mrw770

8:35 pm on Apr 17, 2003 (gmt 0)

10+ Year Member



Thanks - it worked! And thanks for the welcome. :)

gcross

10:26 pm on Apr 17, 2003 (gmt 0)

10+ Year Member



When I use external javascript files for menus, copyrights, updated lines, etc., this is the code I use:

<script type="text/javascript" language="Javascript" src="filename.js">
</script>

This goes into the BODY into the appropriate placement and does not need to be echoed in the HEAD section. Then the file named filename.js contains the content.

To create it is pretty easy. Insert your content into the main document and format it until it looks the way you want it. Then remove that code, complete with formatting codes and put it into the filename.js file and add in the necessary javascript changes into the .JS file and the <script> command into the .HTML file. I generally rely on the
document.write (" ");
command line for my external .JS file. Don't forget to comment out the quotes that are a part of the navigation, with a backslash (\), like
document.write ("<a href=\"filename.htm\"></a>");

And to make sure the spiders and Linux users are not completely confounded by the javascript menus, I try to have at least the index page containing the menus unadulterated by javascript.

grahamstewart

11:16 pm on Apr 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found a really great free javascript to replace the menus on my website

I'd just like to emphasise what gcross pointed out - if you use javascript for all your menus then people with javascript turned off will not be able to navigate your site, and neither will spiders.

Make sure you supply an alternative means of navigation - or consider using CSS instead of Javascript to make pretty looking menus (depends what you want it to do really, but CSS can handle all the usual prety hover effects).

grahamstewart

11:20 pm on Apr 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh.. and if you are writing a page using html that validates [validator.w3.org] to a recent doctype (and you should be!), then omit the language="Javascript" tag, it has been deprecated.

ShawnR

2:00 am on Apr 18, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"...Don't forget to comment out the quotes that are a part of the navigation, with a backslash (\), like
document.write ("<a href=\"filename.htm\"></a>"); ..."

It is a good idea to get in the habit of using one set of quotes for html and the other for javascript. For example:

document.write ('<a href="filename.htm"></a>');

You still might need to escape quotes from time to time, depending on the context, but much less often, and the code will be easier to read and debug.

Shawn

g1smd

1:00 pm on Apr 20, 2003 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Make sure that you provide a normal HTML link to your sitemap page, and that the sitemap page has normal HTML links to every other page of your site. This helps both search engines which cannot follow javascript links, as well as users who have turned off javascript in their browser, or who never had it installed in the first place.

You also need to escape the / of the closing </a> tag, and of any other closing tags, by changing to this format: <\/a> instead.

 

Featured Threads

Hot Threads This Week

Hot Threads This Month