Click to See Complete Forum and Search --> : Replacing Varriables With JS
ShadowPhantom
04-28-2006, 08:42 PM
Im Disigning a site and I want Every Page to Look the Same But I dont want to Paste the Same code On Every Page I was wondering If Theres a Way To Use Javascript to Read the Page and replace a variable in it with a text file...
To make it more clear:
I Want to Be able to Write a Page Like:
<html>
*Header*
<body>
*bodyformatting*
(Id Write out the Content Here)
*bodyformattingclose*
</body>
</html>
and Have everything inside the "**" Be Replaced With HTML I Write in a txt file in the same folder as the HTML Document
David Harrison
04-28-2006, 08:47 PM
Server side includes or any server side language could do that no problem. But you need a host that supports one of the two.
Freewebs is a free webhost that I think has support for server side includes, that is if you don't already have a host.
BuilderQ
04-28-2006, 08:59 PM
Create a file with content like varname="Your content for all pages" and save it with .js extension.
In the head of every page, place this: <script type="text/javascript" src="whateveryounamedyourjsfile.js"></script>
In the body of every page, place this where you want your content to appear:
script type="text/javascript">document.write(varname)</script>
David Harrison
04-28-2006, 09:57 PM
I strongly advise against doing that. Making your pages rely on JavaScript will shut out 5% (1 in 20) of people surfing the web, not to mention search engine spiders.
http://www.thecounter.com/stats/2006/April/javas.php
Not everyone on the web has JavaScript enabled, some have it disabled by choice, some don't have the option of enabling it on their browser.
JavaScript should be used to enhance the page and make it more user friendly, but if JavaScript is disabled, the page should still work fine. Whatever you do, don't allow your pages to become dependant on JavaScript.
Here's what the W3C have to say on the matter in the Web Content Accessibility Guidelines (http://www.w3.org/TR/WAI-WEBCONTENT/):6.3 Ensure that pages are usable when scripts, applets, or other programmatic objects are turned off or not supported. If this is not possible, provide equivalent information on an alternative accessible page. [Priority 1]
For example, ensure that links that trigger scripts work when scripts are turned off or not supported (e.g., do not use "javascript:" as the link target). If it is not possible to make the page usable without scripts, provide a text equivalent with the NOSCRIPT element, or use a server-side script instead of a client-side script, or provide an alternative accessible page.
Yes, a server include is the best method (php is one of the most common accepted server-side language on hosts).
You may also consider iframes, but if you care about the search engines (which mostly look both for metatags and for the content) choose the server includes.
BuilderQ
04-29-2006, 09:13 PM
Something that can be described as *bodyformatting* probably doesn't have to be indexed by search engines, as it is not the actual page content.
David Harrison
04-30-2006, 01:58 AM
Correct, *bodyformatting* probably doesn't require search engines to index it as it is not actual content. Using JavaScript to write it out though, is it really worth blocking out 1 in 20 visitors simply because you wanted to save a bit of time? And if a server side include were used it wouldn't cost any extra time at all.
Why cut a corner when there is a much more accessible alternative solution available that wouldn't require any extra effort?