473,410 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Server Side include to replace text

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
5 4737
"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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: David W. Fenton | last post by:
A client is panicking about their large Access application, which has been running smoothly with 100s of thousands of records for quite some time. They have a big project in the next year that will...
1
by: Matthew Louden | last post by:
Personally, I am totally confused with the following control terms and usage and advantages of each one in ASP.NET web application. Here's what I know so far.. 1. HTML Client Control: understood...
12
by: James Lankford | last post by:
Hello group: I have reached the end of my rope with this error. I have pretty much tried every suggestion I've found thus far and I get this error no matter what I do. I did try using the CDOSYS...
3
by: Andla Rand | last post by:
Hi, I would appreciate some directions on how to replace text as server controls. I'm currently reading text from a database and would like to build server logic based on specific information...
3
by: Larry | last post by:
Hi, I need a method to dynamically include a server side include in my asp.net page. The problem is, the include file contains asp.net controls, and I can't find a way to get the controls to...
2
by: pamelafluente | last post by:
Hi dears, I have a plain HTML page. I want to render it a little interactive. I was thinking to add to it 1 script and events to the elements I want to make interactive. Then, I need to...
2
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
0
by: ankuragt | last post by:
(problem in writing a string into a file on client side.) hey i have written a code of server and client.what i want to do is to transfer the contents of file on server side to the client side (line...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.