...

View Full Version : change some letters from a paragraph when i load in a page



kamkam
03-14-2009, 12:46 PM
Hi;
i'd like to change some letters from a paragraph when i load in a page.

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

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


<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>

Philip M
03-14-2009, 01:12 PM
Hi;
i'd like to change some letters from a paragraph when i load in a page.

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




Why on earth not? If you want it to work, delete this:-


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

Change to:

<body onload = "s2T()">

The point of this still eludes me.


"Before a man speaks it is safe to assume that he is a fool. After he speaks it is seldom necessary to assume." - H.L. Mencken 1880-1956, American Editor, Author, Critic, Humorist

kamkam
03-14-2009, 01:29 PM
I just want to known why i can not use javascript function instead of the onLoad(), that's all i want.

Philip M
03-14-2009, 01:37 PM
I just want to known why i can not use javascript function instead of the onLoad(), that's all i want.

Because, as you seem to have discovered, it does not work. OK?
And it does not work because the function s2t() does not exist at the moment it is called.

You could call it with
s2t();
</script>
right in front of the </script> tag. But this is not always reliable depending on other content of the page.

kamkam
03-14-2009, 01:46 PM
ok, thanks for your time



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum