By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,849 Members | 2,169 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,849 IT Pros & Developers. It's quick & easy.

Server Side include to replace text

P: n/a
Hello,
Can someone give me specific code to replace text on a page using
server side javascript? I need to use server-side because I need the
output to be recognized in the final HTML so that google can index it.

Here is a specific example of what I want to do:

<div id=SomeText>
Here is some text. I went to the baseball game
</div>

I want some server-side javascript that would replace 'baseball" with
<a href="www.espn.com">baseball</a>

That way the final output of the HTML (as recognized by google or View
Source in IE) would show the following:


<div id=SomeText>
Here is some text. I went to the <a
href="www.espn.com">baseball</a>game
</div>

Any suggestions?

thanks,
Carson

Feb 6 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Casey" <ca***@nightingale.com> wrote in news:1139268994.627198.284760
@z14g2000cwz.googlegroups.com:
<div id=SomeText>
Here is some text. I went to the <a
href="www.espn.com">baseball</a>game
</div>

Any suggestions?


yes, a server-side scripting language, like PHP,.NET, or Cold Fusion.
Feb 7 '06 #2

P: n/a

Using PHP, I'd have PHP do all that, open the file stream with
fopen(), pass it to a variable with fread(), and run over it
preg_replace() for the 'baseball' criterion, then Echo it out and
serve it :) .
Danny
Feb 7 '06 #3

P: n/a
Casey wrote:
Can someone give me specific code to replace text on a page using
server side javascript? I need to use server-side because I need the
output to be recognized in the final HTML so that google can index it.

Here is a specific example of what I want to do:

<div id=SomeText>
Here is some text. I went to the baseball game
</div>

I want some server-side javascript that would replace 'baseball" with
<a href="www.espn.com">baseball</a>


Why, methods of the core language work in SSJS as well:

var htmlSource = [
'<div id="SomeText">',
'Here is some text. I went to the baseball game',
'</div>'
].join('\n');

write(htmlSource.replace(
/\b(baseball)\b/,
'<a href="www.espn.com">$1</a>'));

In case you need to match words that begin or end with non-ASCII letters or
special characters, use

/(^|[^\w])(ben|da)([^\w]|$)/

and the like.
HTH

PointedEars
Feb 7 '06 #4

P: n/a
I have only used Client-side Javascript. How do I impliment it
Server-Side? Also, is there a way to get the HTML into the variable
rather than having to define it in the function as you did? I want the
designers to be able to create the HTML in dreamweaver and just know to
put a <div id=Replace> or something like that around the text they
enter and then the server side script will take care of doing the
replacing.

How would I do this? something like...
<html>
<body>
<div id=Replace>
Here is some text. I went to the baseball game
</div>
<server>
var htmlSource = [
document.getElementById(Replace).innerHTML;
].join('\n');
write(htmlSource.replace(
/\b(baseball)\b/,
'<a href="www.espn.com">$1</a>'));

</server>
</body>
</html>

Thanks for your help

Feb 7 '06 #5

P: n/a
Casey wrote:
I have only used Client-side Javascript. How do I impliment it
Server-Side? Also, is there a way to get the HTML into the variable
rather than having to define it in the function as you did?
I did not define the HTML code in a function but in an Array object.
I want the designers to be able to create the HTML in dreamweaver and just
know to put a <div id=Replace> or something like that around the text they
enter and then the server side script will take care of doing the
replacing.
I think you will have to access a file containing this
HTML code and store the code in the variable. See below.
How would I do this? something like...
<html>
<body>
<div id=Replace>
Here is some text. I went to the baseball game
</div>
<server>
var htmlSource = [
document.getElementById(Replace).innerHTML;
].join('\n');
write(htmlSource.replace(
/\b(baseball)\b/,
'<a href="www.espn.com">$1</a>'));

</server>
</body>
</html>


Certainly not. First you have to distinguish core language and DOM
(Document Object Model). Almost standards-compliant
document.getElementById() and the proprietary `innerHTML' property are
features of the DOM API, provided by the host environment of a HTML
user agent (colloq.: Web browser). They certainly are not available
for server-side scripting. And the code you generate should be Valid.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Let's Talk About Baseball</title>
</head>

<body>
<div>

<server>
// or you retrieve the HTML code from a file, see below
var htmlSource = [
' <div>',
' Here is some text. I went to the baseball game',
' </div>'
].join('\n');

write(htmlSource.replace(
/\b(baseball)\b/,
'<a href="www.espn.com">$1</a>'));
</server>

</div>
</body>
</html>

(I am using this indentation style to indicate what happens server-side
and what is received by the client.)

Another quickhack on file access (I do not have SSJS available):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<!-- it's Transitional here only to allow authors to use
features marked as deprecated in their includes -->
<html>
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<title>Let's Talk About Baseball</title>
</head>

<body>

<server>
var f = new File("/home/user/public_html/foo.html"), htmlSource = [];
if (f && f.open("r"))
{
while (!f.eof())
{
// fill the buffer with data chunks of max. 4096 characters;
// f.readln() is more intuitive but probably less efficient
htmlSource.push(f.read(4096));
}
f.close();
}

if (htmlSource.length > 0)
{
// join the chunks in the buffer and replace key word(s)
htmlSource = htmlSource.join("")
.replace(/\b(baseball)\b/, "<a href="www.espn.com">$1<\/a>");

// generate content
write(htmlSource);
}
</server>

</body>
</html>

See also

<URL:http://research.nihonsoft.org/javascript/jsref/>
<URL:http://research.nihonsoft.org/javascript/jsref/oth1.htm>

However, the challenge here is to replace the word (here: "baseball")
only if it occurs within a div[id="Replace"] element. A context-free
non-regular language, like a markup language where every start tag must
have a matching end tag (to delimit the respective element) cannot be
parsed with one application of one Regular Expression (ref.: Chomsky
hierarchy). So either you will have to impose constraints on the
context (such as that those elements must not contain other elements)
or to implement a non-deterministic PDA (Push-Down Automaton) that can
be used to parse this context-free language accordingly.

For the former, the following should suffice (it does with Core
JavaScript 1.6 in Firefox 1.5/Linux):

... htmlSource.replace(
/(<div )id=Replace([^>]*>[^<]*)\b(baseball)\b([^<]*<\/div>)/gi,
"$1$2<a href="www.espn.com">$3<\/a>$4") ...
HTH

PointedEars
Feb 7 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.