Newbie query: Centrally set variables?

W

Wayne Stuart

Simple question...

On my website, I need to display a number on several pages, which
changes periodically. Now, rather than go through every page every
time
this number changes, is there some bit of Javascript or something that
can read some centrally set variable for this number and display it
where
needed?

For example, if I have 2 pages where one says, "There are 30 pages in
this
website", and another page says "Any one of the 30 pages in this site
will help", how can I get the "30" to appear on both without having to
change both?

Oh, and can you do maths on this number, e.g. display 30-1?

Idiots guide explanation please as I'm an absolute newbie when it
comes to Javascript.

Thanks.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Sun, 18 Mar 2007 04:47:16, Wayne Stuart
Simple question...

On my website, I need to display a number on several pages, which
changes periodically. Now, rather than go through every page every
time
this number changes, is there some bit of Javascript or something that
can read some centrally set variable for this number and display it
where
needed?

For example, if I have 2 pages where one says, "There are 30 pages in
this
website", and another page says "Any one of the 30 pages in this site
will help", how can I get the "30" to appear on both without having to
change both?

Oh, and can you do maths on this number, e.g. display 30-1?

Idiots guide explanation please as I'm an absolute newbie when it
comes to Javascript.

SSI, if available, would do it.

But, if you already knew about SSI then you would not have asked.

It can also easily be done with javascript include files, and ISTM that
if you already knew about those then you would not have asked.

Every site should be able to use include files, but ISPs do not
necessarily provide SSI with Web hosting; and include files are easy to
use and considerably useful.


At a minimum, you could put just put var PageCount = 30 in a file
called include2.js and put
<script type="text/javascript" src="include2.js"></script>
in the head of each page with such as
<script type="text/javascript">
document.write(" Contents ", PageCount, " Pages. ")
</script>
in the pages themselves.

See <www.merlyn.demon.co.uk/js-nclds.htm>
 
W

Wayne Stuart

Here is how to insert text into a web page with JS:

There are
<SCRIPT TYPE="text/javascript">
var numberOfPages = 30;
document.write(numberOfPages);
</SCRIPT>
pages in this website.

Now if you move the variable to a separate file, named numberFile.js,
which contains this one line:

var numberOfPages = 30;

then you can include that file in each web page

<script type="text/javascript" src="numberFile.js" >
</script>

and the JS on each page is the same:

There are
<SCRIPT TYPE="text/javascript">
document.write(numberOfPages);
</SCRIPT>
pages in this website.

Perfect! Just what the doctor ordered. Thanks.

One day, I will learn javascript... right after I've done CSS. ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top