/    Sign up×
Community /Pin to ProfileBookmark

managing same text appearing on different pages

Hi,

Is there a way I can manage the same text that appears on different pages.

For example: I would like to put a phone number on different pages, but want to be able to change it from one main file, and not having to find/reprogram all involved pages.

I want to keep my pages in html, so php isn’t an option. Is there a way with Javascript?

Cheers,
Pieter

to post a comment
JavaScript

18 Comments(s)

Copy linkTweet thisAlerts:
@PittimannMar 13.2004 — Hi!

Yes - it's possible, but you should consider, that approx. 13% of all users do not have javascript enabled...

Want some code?

Cheers - Pit
Copy linkTweet thisAlerts:
@pieterhauthorMar 13.2004 — Please ?
Copy linkTweet thisAlerts:
@PittimannMar 13.2004 — Hi!

Just put this in an empty file and save it as "phone.js":

var myNumber='0123456789';

Then you will need the .htm file (has to be situated in the same folder like phone.js):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title>Untitled</title>

<script language="JavaScript" type="text/javascript" src="phone.js"></script>

<script language="JavaScript" type="text/javascript">

<!--

function showNumber(){

document.getElementById('myPhone').innerHTML=myNumber;

}

//-->

</script>

</head>

<body onload="showNumber()">

blah blah...<br>

and my telephone number is: <span id="myPhone"></span><br>

more contents here...

</body>

</html>

It seems, you might need the js file to be stored somewhere else, because you want to deal with multiple pages using the variable. Just exchange the src="phone.js" in the first script tag with the full path and file name of the js file and there you are...

Cheers - Pit
Copy linkTweet thisAlerts:
@steelersfan88Mar 13.2004 — referring to Pit's code, you might want to add the function into the .js file also since you will be using that more than once. Just make sure that the Id of the field (div, span, etc..) being displayed is constant, such as this as .js file:[code=php]var myNumber='0123456789';

function showNumber(){
document.getElementById('myPhone').innerHTML=myNumber;
}[/code]
and this is an example of page for display:[CODE]<body onload="showNumber()">
blah blah...<br>
and my telephone number is: <span id="myPhone"></span><br>
more contents here...
</body>[/CODE]
of course referencing to the .js file as Pit did in the above the above reply
Copy linkTweet thisAlerts:
@pieterhauthorMar 13.2004 — Working great!

One more question..

What if I also have my address and the name of my girlfriend (changes often, haha).

What do I put in body-tag?

Other steps are clear.. just put my idea underneath...

Cheers,

Pieter



First I should add to .js file type (I guess):

var myAddress='Street';

var myGirlfriend='Jennifer';

Then (I guess):

[code=php]
<script language="JavaScript" type="text/javascript">
<!--
function showNumber(){
document.getElementById('myPhone').innerHTML=myNumber;
}

function showAddress(){
document.getElementById('myStreet').innerHTML=myAddress;
}

function showGirlfriend(){
document.getElementById('mySweetie').innerHTML=myGirlfriend;
}
//-->
</script>
[/code]


In body:
[code=php]
<span id="myAddress"></span><span id="mySweetie"></span>


[/code]
Copy linkTweet thisAlerts:
@steelersfan88Mar 13.2004 — first, add the functions to the .js file without the script tags.

i would change it to all one function, showInfo(). and call the showInfo() function for body onload:[code=php]var number = "..."
var address = "..."
var girlfr = "..."

function showInfo() {
// three document.getElement... lines here
}[/code]
in the js file, then just have the following in the html pages[code=php]<script type="text/javascript" src="whereISavedIt.js"></script>

...

<body onload="showInfo()">
<spans here>
more content, blah, blah, blah[/code]
Copy linkTweet thisAlerts:
@pieterhauthorApr 09.2004 — Hi,

Now I would like to reuse same item on same page. But when I do this it only shows it once.

Cheers,

Pieter
Copy linkTweet thisAlerts:
@steelersfan88Apr 09.2004 — How do you plan to reuse it? If making the source correctly, you will be able to access it by the name you gave the variable ?
Copy linkTweet thisAlerts:
@pieterhauthorApr 09.2004 — I use following tag more than once on a page:

[code=php]

<span id="item01"></span><span id="item01"></span>

[/code]


It will not show second span tag.
Copy linkTweet thisAlerts:
@steelersfan88Apr 09.2004 — ok, i see what you mean. You will have to rename the second span element, then in the javascript, add a line exactly the same to the others, except with the name of the new span element ?
Copy linkTweet thisAlerts:
@pieterhauthorApr 09.2004 — but then when I change data.. I have to change maybe 20 different lines... is that not a bit complicated?

Cheers,

Pieter
Copy linkTweet thisAlerts:
@steelersfan88Apr 09.2004 — if you change the data, it will be ok. it will just need to change the name of the element:[code=php]var item1 = "a"
var item2 = "b"
var item3 = "c"
var item4 = "d"
var item5 = "e"

document.getElementById('num1').innerHTML = item1
document.getElementById('num2').innerHTML = item2
document.getElementById('num3').innerHTML = item3
document.getElementById('num4').innerHTML = item4
document.getElementById('num5').innerHTML = item5
document.getElementById('num6').innerHTML = item1
document.getElementById('num7').innerHTML = item2[/code]
That reuses the items 1 and 2, but if the data changes, it only requires changing them one time ?
Copy linkTweet thisAlerts:
@pieterhauthorApr 09.2004 — Great, I got it to work!

Thanks,

Pieter
Copy linkTweet thisAlerts:
@steelersfan88Apr 09.2004 — good to hear ?
Copy linkTweet thisAlerts:
@pieterhauthorApr 13.2004 — Now I run into another problem.

[code=php]

var price='<a href="abc.pdf" OnClick="alert('Use right mouseclick and select ''save target as''.');return false;">Download ABC</a>';

[/code]


But because of the '-signs within the line of code I get an error.

Can you give me advice.

Cheers,

Pieter
Copy linkTweet thisAlerts:
@PittimannApr 13.2004 — Hi!

steelersfan88 told me, he will be busy for some days. Maybe, the attachment helps (sorry - I am fed of finding out, how many times to escape signs here). In the text file, you should have, what I intended...

Cheers - Pit

[upl-file uuid=46cd4bb7-91ea-41a4-a36b-f8c53ff5df41 size=388B]pieterh.txt[/upl-file]
Copy linkTweet thisAlerts:
@steelersfan88Apr 13.2004 — I find time in my schedule, but you started this thread and I joined in, so go right ahead and take over my job ?

You simply have to escape the single quotes in the alert, that's all, if you didn't already or don't feel like downloading the attachment ?
Copy linkTweet thisAlerts:
@pieterhauthorApr 14.2004 — Hi,

Thanks for the solution using &quot; notation instead op '-sign.

Cheers,

Pieter
×

Success!

Help @pieterh 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,
)...