Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

change some letters from a paragraph when loading a page 1

Status
Not open for further replies.

xbl12

Programmer
Dec 12, 2006
66
Hi;
i'd like to change some letters from a paragraph when i load in a page.

I can do in the following two ways, it works fine. but i do want the following way.
<body onLoad="s2t()">

and i write my code as following, but it does not work, could anyone help, please.

Code:
<html>
<head>

</head>
<body>
<script type="text/JavaScript"> 
 s2t();
</script>


<div id="mydiv">
Vince Cervi, 41, who once held the Australian heavyweight title belt, 
died from a wound to the abdomen at Preston, after the alleged confrontation reports the Herald Sun. 
</div>
<script type="text/JavaScript"> 
function s2t(){ 
alert("welcome to y");
var oldchars = new Array("A","B","C","D","E","a","b","c","d","e"); // original letters
var newchars = new Array("N","Y","G","L","W","n","y","g","l","w");  // letters to change to
var mytext = document.getElementById("mydiv").innerHTML;
var oldlen = oldchars.length;
for (var i = 0; i < oldlen; i++) {
var changefrom = oldchars[i];
var change2 = newchars[i];
var replaceVal = new String(changefrom); 
var match = new RegExp(replaceVal, 'g');	 
mytext = mytext.replace(match, change2); 
}
document.getElementById("mydiv").innerHTML = mytext;
} 
</script> 
</body>
</html>
 
Whatever change to s2t(), relative to your previous thread, you cannot use it bare before the page is rendered properly.

Change the line:
>s2t();
to this:
[tt]window.onload=function() {s2t()};[/tt]
 
I'd love to know what possible reason you have for switching letters around on a web page in this way. I can't think of any serious application for it, at least...

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks a lot, i do understand it now.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top