Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts

    change some letters from a paragraph when i load in a page

    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.

    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>

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,175
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    Quote Originally Posted by kamkam View Post
    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

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    I just want to known why i can not use javascript function instead of the onLoad(), that's all i want.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,175
    Thanks
    203
    Thanked 2,550 Times in 2,528 Posts
    Quote Originally Posted by kamkam View Post
    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.
    Last edited by Philip M; 03-14-2009 at 06:21 PM. Reason: typo

  • #5
    Regular Coder
    Join Date
    Dec 2006
    Posts
    306
    Thanks
    24
    Thanked 0 Times in 0 Posts
    ok, thanks for your time


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •