Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I was wondering if there was a way with Javascript to replicate redundant data across web pages.
For example I have a side links bar that will be the same on every single page. Is there way to create a text file perhaps with the HTML in it and "import" that file to each page as it is loaded (sort of the way you @import a .css file)? That way I do not have to copy/paste the information on each page and it would be changeable from only one place and not have to go page by page?

Yes, you can do it, but that is what Server Side Include files are for! Of course, the hosting server must be configured to support them. Check with your ISP on that.
If you must go the JavaScript router, there will be issues if anyone has JavaScript disabled and possible cross browser compatibility issues. But, to keep it simple, just put the following code where you want the code for your menu:
<script language="javascript" src="jsmenu.js">
Of course the filename can be whatever you want it to be, just remember to include the relative path if it is in a different directory. Then in your jsmenu.js file either have a bunch of document.write's or (my preference) build a variable then do a single document.write, such as:
menuStr = 'This is the start of the menu[br]';
menuStr = 'This is the middle of the menu[br]';
menuStr = 'This is the end of the menu[br]';
document.write(menuStr);(The less than/greater than signs around the break tags were changed to brackets for this example)

Awesome, that worked! While the file is big, it's a lot better than having all that redundant information in HTML files. Quick question though. In the document writes, does a / need to be proceeded by a \.
For example:
[a href="test.html"]Click[\/a]
or could I:
[a href="test.html"]Click[/a](The less than/greater than signs around the break tags were changed to brackets for this example)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |