473,763 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4785
"Casey" <ca***@nighting ale.com> wrote in news:1139268994 .627198.284760
@z14g2000cwz.go oglegroups.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(htmlSourc e.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.getEle mentById(Replac e).innerHTML;
].join('\n');
write(htmlSourc e.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.getEle mentById(Replac e).innerHTML;
].join('\n');
write(htmlSourc e.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.getEle mentById() 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(htmlSourc e.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.len gth > 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(htmlSourc e);
}
</server>

</body>
</html>

See also

<URL:http://research.nihons oft.org/javascript/jsref/>
<URL:http://research.nihons oft.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.repl ace(
/(<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
3826
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 lead to a lot of use of the database and the adding of quite a lot of new data (though I can't conceive of them adding more than than 10s of thousands of records, which won't change the current performance profile at all). If there is a SQL...
1
2533
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 by the client only. e.g. <input type="text" name="emp_name"> 2. HTML Server Control: understood by the server only. e.g. <input type="text" name="emp_name" runat="server"> 3. Web Server Control: understood by the server only. e.g. <asp:TextBox
12
10374
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 test from Microsoft as a test for the smtpserver, and it'll send that way. Here's my existing code: Public Sub SendMail() Dim myMailMsg As New Mail.MailMessage()
3
2066
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 retrieved from the server. Here is some sample code that I'm currently playing with: string repl=spn.InnerHtml;
3
1597
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 render. HISTORY I have an active website. I have about 100 include files that are just html text. In .aspx page, I have the following:
2
1922
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 talk with an ASP.NET page which would actually do some processing on the server. Processing may include a replace (regeneration) of the above HTML page, for further interaction.
2
6965
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 attending interviews. If you own a company best way to judge if the candidate is worth of it. http://www.questpond.com/InterviewRatingSheet.zip
0
1691
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 by line).. the code for server is given as /* A simple server in the internet domain using TCP The port number is passed as an argument */ #include <iostream.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include...
0
9387
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9938
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8822
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7368
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.