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 9 of 9
  1. #1
    New Coder
    Join Date
    Sep 2006
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Loading replace method on window.onload

    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!

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

  • #2
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by sagaticus View Post
    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!

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

    Code:
    function replaceChars(obj)
    {
    	obj.innerHTML = obj.innerHTML.replace(/™/g,"&reg;");
    }
    
    <body onload="replaceChars(this)">
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #3
    New Coder
    Join Date
    Sep 2006
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks for your help... I tried the following and had no luck, does this work for you?

    Code:
    <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?

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    This works
    Code:
    <!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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    New Coder
    Join Date
    Sep 2006
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile

    Thank you very much for your help. 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...

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i ask now why you are not doing this with a server side language like php?

    it would be very easy.
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<

  • #8
    New Coder
    Join Date
    Sep 2006
    Posts
    24
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #9
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    public string ConjunctionJunction(string words, string phrases, string clauses)
    {
    return (String)(words + phrases + clauses);
    }
    <--- Was I Helpfull? Let me know ---<


  •  

    Posting Permissions

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