Connecting Tech Pros Worldwide Forums | Help | Site Map

Server Side include to replace text

Casey
Guest
 
Posts: n/a
#1: Feb 6 '06
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[color=blue]
> Source in IE) would show the following:[/color]

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

Any suggestions?

thanks,
Carson


Good Man
Guest
 
Posts: n/a
#2: Feb 7 '06

re: Server Side include to replace text


"Casey" <casey@nightingale.com> wrote in news:1139268994.627198.284760
@z14g2000cwz.googlegroups.com:
[color=blue]
> <div id=SomeText>
> Here is some text. I went to the <a
> href="www.espn.com">baseball</a>game
> </div>
>
> Any suggestions?[/color]

yes, a server-side scripting language, like PHP,.NET, or Cold Fusion.
Danny
Guest
 
Posts: n/a
#3: Feb 7 '06

re: Server Side include to replace text



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
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#4: Feb 7 '06

re: Server Side include to replace text


Casey wrote:
[color=blue]
> 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>[/color]

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
Casey
Guest
 
Posts: n/a
#5: Feb 7 '06

re: Server Side include to replace text


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

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Feb 7 '06

re: Server Side include to replace text


Casey wrote:
[color=blue]
> 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?[/color]

I did not define the HTML code in a function but in an Array object.
[color=blue]
> 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.[/color]

I think you will have to access a file containing this
HTML code and store the code in the variable. See below.
[color=blue]
> 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>[/color]

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
Closed Thread