...

View Full Version : Loading replace method on window.onload



sagaticus
09-05-2006, 09:49 PM
This forum has helped me so far with what I need to do. I need to replace special chars in my site that were entered using ALT+0153 or ALT+0174 for tradmarks and register marks. I need to turn these into correct HTML characters. I found the code below works fine with the document.write, however I am having difficulty applying this snippit to the rest of the page outside the <head> tags. I appreciate your help!


<script type="text/javascript">
<!--//
function convertTM(string){
return string.replace(/™/g,"&reg;");
}

// EXAMPLE USAGE:
document.write(convertTM('It™s is a test. It™s is a test.'));

//-->

</script>

rlemon
09-05-2006, 10:51 PM
This forum has helped me so far with what I need to do. I need to replace special chars in my site that were entered using ALT+0153 or ALT+0174 for tradmarks and register marks. I need to turn these into correct HTML characters. I found the code below works fine with the document.write, however I am having difficulty applying this snippit to the rest of the page outside the <head> tags. I appreciate your help!


<script type="text/javascript">
<!--//
function convertTM(string){
return string.replace(/™/g,"&reg;");
}

// EXAMPLE USAGE:
document.write(convertTM('It™s is a test. It™s is a test.'));

//-->

</script>
try this



function replaceChars(obj)
{
obj.innerHTML = obj.innerHTML.replace(/™/g,"&reg;");
}

<body onload="replaceChars(this)">

sagaticus
09-05-2006, 11:10 PM
Thanks for your help... I tried the following and had no luck, does this work for you?


<head>
<script type="text/javascript">
<!--//
function replaceChars(obj)
{
obj.innerHTML = obj.innerHTML.replace(/™/g,"&reg;");
}
//-->
</script>
</head>
<body onload="replaceChars(this);">
<p>ChangeTmToReg™</p>
</body>
</html>

Any suggestions?

_Aerospace_Eng_
09-05-2006, 11:20 PM
This works

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<script type="text/javascript">
function convertTM(string)
{
return string.replace(/™/g,"&reg;");
}
window.onload = function()
{
var bodyNode = document.getElementsByTagName('body')[0];
bodyNode.innerHTML = convertTM(bodyNode.innerHTML);
}
</script>
</head>

<body>
<p>ChangeTmToReg™</p>
<p>ChangeAnotherTmToReg™</p>
<p>AndAnotherTmToReg™</p>
</body>
</html>
There is probably a better method for getting the HTML but I'm sure what that would be.

sagaticus
09-06-2006, 02:59 AM
Thank you very much for your help. :thumbsup: This works perfect for FFox and IE, but I can't get this to work in Safari. After the page loads, the javascript attempts to replace the characters and at that point it goes blank. Can this work in Safari somehow? Thanks...

_Aerospace_Eng_
09-06-2006, 03:54 AM
It might be that safari doesn't support innerHTML so you will need to use DOM to get the contents of the body tag. Look into using nodeValue.

rlemon
09-06-2006, 03:36 PM
i ask now why you are not doing this with a server side language like php?

it would be very easy.

sagaticus
09-06-2006, 06:23 PM
I typically use php/mysql CMS implementations but for this application I'm actually using a win2k3 box to run an XML based CMS. This CMS is having problems with parsing special characters. So this javascript patch should work for the time being.

I'd be curious to see how this will work with php. Would you be able to post an example? I can always install php if need be. Thanks again.

rlemon
09-06-2006, 07:38 PM
well, just off the top of my head (i'm sure there are better ways if you ask or look around)

you will have your .htm or .html page with your content. you now just need a 'parser' page to load it into.

for example:

index.php?page=path/to/page1.htm

<?php
if(isset($_REQUEST['page']))
{
if (file_exists($_REQUEST['page']))
{
print preg_replace("/™/g", "&reg;", file_get_contents($_REQUEST['page']));
} else {
print "page does not exist"; // again you may want to set a default 'page' value here.
}
} else
{
print "No Page"; // you can replace this with a common html page. i.e home
}
?>

now if you look at what i've done there. i now take the file path from the url. see if the file exists, and output the 'replaced' version of the text.
however there may be a method you can call before the page is rendered to replace it all from the same page. i'm not sure. I know this can be done using ASP but PHP ??? Anyone?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum