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