SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    Idea Developer
    Join Date
    Sep 2000
    Location
    Bethlehem, PA
    Posts
    521
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    text replace problem, doesn' work in IE

    After the HTML page finishes loading, I want the JavaScript code to go tthrough all the text and replace some words.

    The following code works in FF

    Code:
      x=document.body.innerHTML.replace(/CC/gi,'Credit Card'); 
      document.body.innerHTML = x;
    but refuses to work in IE. It gives a "Operation Aborted" message and does not even render the orginal HTML.

    Any suggestions? .. Maybe I am going wrong about the entire thing, all i want is to alter text (not tags) on the page using JavaScript.

    Thanks for any help or insight into the matter.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works for me in both IE and Mozilla:
    Code:
    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    window.onload = function()
    {
      var sHTML = document.body.innerHTML.replace(/CC/gi,'Credit Card'); 
      document.body.innerHTML = sHTML;
    }
    </script>
    </head>
    
    <body>
    <p>Credit Card - replaced</p>
    <p>Dog - Not replaced</p>
    
    
    </body>
    </html>

  3. #3
    Idea Developer
    Join Date
    Sep 2000
    Location
    Bethlehem, PA
    Posts
    521
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Thank you very much, that does work. However, what I am trying to accomplish may be perhaps harder then I expected. I've been looking up and hacking code but no luck in getting it to consistently work.

    Goal:
    Search text of page for stock symbols (i have in an array).
    Replace plain text of "blah blah YHOO blah" with "blah blah YHOO blah" but that YHOO and make it a link to http://finance.yahoo.com/q?s=YHOO , where after stock symbol atomatically filled in from the one found

    Code found:
    If the stock symbols are contains in a href or a div, there is plenty of code to search and replace .. ex

    Code:
     
     var i, m, a, l, o, links, tag, req = "";
     		
     		// check the document for tags... look for anchor tags in
     		// the page with our URL
     		
     		links = Object();
     		a = document.getElementsByTagName( "a" );
     		for( i = 0; i< a.length; i++ )
     		{
     			if( a[i].rel == "tag" && a[i].href )
     			{
     				if( m = a[i].href.match( /^http:\/\/[www.]*xxx\.com\/tag\/(.+)$/ ))
     				{
     					tag = this.scrubTag( m[1] );			
     					links[ tag ]++;
     				} 
     			}
     		}
    but how can I modify that code to search through the text of the entire page, not only the text in the tags.

    Any suggestions would be appreciated...

    Thanks!


Bookmarks

Posting Permissions

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