/    Sign up×
Community /Pin to ProfileBookmark

Replacing Varriables With JS

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:

[CODE]
<html>
*Header*
<body>
*bodyformatting*

(Id Write out the Content Here)

*bodyformattingclose*
</body>
</html>[/CODE]

and Have everything inside the “**” Be Replaced With HTML I Write in a txt file in the same folder as the HTML Document

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonApr 29.2006 — 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.
Copy linkTweet thisAlerts:
@BuilderQApr 29.2006 — 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: &lt;script type="text/javascript" src="whateveryounamedyourjsfile.js"&gt;&lt;/script&gt;

In the body of every page, place this where you want your content to appear:
script type="text/javascript"&gt;document.write(varname)&lt;/script&gt;
Copy linkTweet thisAlerts:
@David_HarrisonApr 29.2006 — 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 [url=http://www.w3.org/TR/WAI-WEBCONTENT/]Web Content Accessibility Guidelines[/url]: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. [color=red][Priority 1][/color]

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.[/quote]
Copy linkTweet thisAlerts:
@KorApr 29.2006 — 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.
Copy linkTweet thisAlerts:
@BuilderQApr 30.2006 — 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.
Copy linkTweet thisAlerts:
@David_HarrisonApr 30.2006 — 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?
×

Success!

Help @ShadowPhantom spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...