Jump to content

Php "includes" File Not Working With Javascript For Menu


lauralee

Recommended Posts

I am using javascript for my website menu. It worked fine as long as I have it typed into each page, but when I started using the includes files in the root directory so that I could us the php script to include it in each website page, the division elements work, but the array for the navigation isn't included.I have placed both the nav.js file and the navigation.inc.html.php file in the root directory. I have also placed the header.inc.html.php and the foot.inc.html.php files in the root directory and they work fine. It is just the navigation list from the javascript file that doesn't work. Below are each of the files I mentioned above.navigation.inc.htm.php<div class="navbox"><script type="text/javascript" src="nav.js"></script></div>nav.js<div class="navbox"><script type="text/javascript">var nav_selections = new Array();nav_selections[0] = "<a class='one' href='http://www.lauralee-llc.com/index.php'>Home</a>";nav_selections[1] = "<a class='one' href='http://www.lauralee-llc.com/about/'>About Us</a>";nav_selections[2] = "<a class='one' href='http://www.lauralee-llc.com/classes/'>Classes</a>";nav_selections[3] = "<a class='one' href='http://www.lauralee-llc.com/supplies/'>Supplies</a>";for (i=0;i<nav_selections.length;i++){document.write(nav_selections+ "          ");}</script></div>It works fine if I place the nav.js file into each individual folder but that defeats the purpose of being able to update the navigation menu from one place. I have tried using additional information in the nav.js line of:<script type="text/javascript" src="nav.js"> to find the source ex. src="/home/lauralee/">, but whatever I've tried hasn't helped.The header, footer, and the division element for the navbox works on all of the pages, just not the navigation menu.Any suggestions?

Link to comment
Share on other sites

I'm not sure if you have one problem here or two. But I can say this. An external javascript file should have nothing in it but javascript. So remove the following from nav.js:<div class="navbox"><script type="text/javascript">...</script></div>Keep only the actual javascript statements (the stuff between the script tags).

Link to comment
Share on other sites

On the other hand, since all of this is wrapped in a PHP include file, why bother having the javascript in an external file at all? Just embed it in the include file.In other words, the thing you are now calling nav.js should be the content of navigation.inc.htm.php, and nav.js can be removed.

Link to comment
Share on other sites

I've edited the include file as shown below. var nav_selections = new Array();nav_selections[0] = "<a class='one' href='http://www.lauralee-llc.com/'>Home</a>";nav_selections[1] = "<a class='one' href='http://www.lauralee-llc.com/about/'>About Us</a>";nav_selections[2] = "<a class='one' href='http://www.lauralee-llc.com/classes/'>Classes</a>";nav_selections[3] = "<a class='one' href='http://www.lauralee-llc.com/supplies/'>Supplies</a>";for (i=0;i<nav_selections.length;i++){document.write(nav_selections+ "          ");}And I have the following code in each page for the navigation.<?php include $_SERVER['DOCUMENT_ROOT'] . '/navigation.inc.html.php'; ?>Where do I put .......<script type="text/javascript"></script> does it go between the css div element?

Link to comment
Share on other sites

If I understand the project, your entire include file would look like this (in outline):<div><script>statements....</script></div>---Now, since you're using the include technique, is there even a reason for JavaScript to write out the menu anymore? Can't you just write out the menu in plain HTML? The effect will be the same, but with fewer layers of code.document.write() was a good technique while you were inserting the menu through a javascript that you linked to in every document. Now that you're using the include technique, you should be able to skip that part.

Link to comment
Share on other sites

I ended up simply creating a table in my database for the navigation I needed and requesting it with php to display on my web pages where I needed it. I don't understand javascript enough to use it effectively, so I'll use php when I can, and wait to use javascript for animation, slide shows etc. But, thanks for you help.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...