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 letter when i click a link 2

Status
Not open for further replies.

xbl12

Programmer
Dec 12, 2006
66
Hi;
I'd like to ask a question.
I got a paragraph, and i want the letter of A will change to N and the letter of P will change to A when i click the
the link of the "click me to change the letter" .

My code as following, but it doesn't do anything, could anyone help, please.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "<html>
<head>
<META HTTP-EQUIV="content-type" CONTENT="text/html; charset=utf-8">
<script type="text/javascript">
function window.onload(){

var s="A P";

var t="N A";

function String.prototype.s2t(){
var k=’’;
for(var i=0;i<this.length;i++) k+=(s.indexOf(this.charAt(i))==-1)?this.charAt(i):t.charAt(s.indexOf(this.charAt(i)))
return k;
}

</script>


<script type="text/JavaScript" language="JavaScript">
function s2t(){
document.body.innerHTML=document.body.innerHTML.s2t();
}

</script>

</head>
<body>
<a href="javascript:s2t()">click me to change the words</a> <br>
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.



</body>
</html>
 
Do you not see any JavaScript errors? Installing Firefox and looking in its error console will show you immediately the problems. I'd also advise installing the Firebug plugin, although it's not necessary to see the errors.

The first is that this:
Code:
function window.onload(){
should be this:
Code:
window.onload = function() {

and similarly, this:
Code:
function String.prototype.s2t(){
should be this:
Code:
String.prototype.s2t = function() {

You're also missing a closing brace for your onload function - which really doesn't need to be there at all (so simply remove the single line which declares it).

Those changes, at least, will get you an error free starting point.

As I said, though - install Firefox and look in its error console - you'll see exactly where your errors are.

Hope this helps,
Dan




Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks for you recommend the error console, it is very usefull.
 
[1] All those put inside window.onload (rightly and wrongly) should go global in scope.
[tt]
<script type="text/javascript">
[red]//[/red]function window.onload(){

var s="A P";

var t="N A";

[red]//[/red]function String.prototype.s2t(){
[red]String.prototype.s2t = function() {[/red]
var k='';
for(var i=0;i<this.length;i++) k+=(s.indexOf(this.charAt(i))==-1)?this.charAt(i):t.charAt(s.indexOf(this.charAt(i)))
return k;
[red]}[/red]
[red]//[/red]}
</script>
[/tt]
[2] Then the change for demo should be more logical to limited to some specific part of the body, for instance, like this. The demo will become with more clarity without destroying all the construction in the body!
[tt]
<a href="javascript:s2t()">click me to change the words</a> <br>
[blue]<div id="divid">[/blue]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.[blue]</div>[/blue]
[/tt]
[3] Then the function s2t change to this accordingly.
[tt]
<script type="text/JavaScript" language="JavaScript">
function s2t(){
[blue]document.getElementById("divid").innerHTML = document.getElementById("divid").innerHTML.s2t(); [/blue]
}
</script> [/tt]
 
Hi tsuji;
Thanks for your times.
You are right, my code looks not logical.

But i want to ask you if i declare the function of the s2t()
in the <head></head>, it will destroying all the construction in the body?



 
No, it won't. Just add confusion to readers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top