www.webdeveloper.com
Recent Articles
  • Finding Slow Running Queries in ASE 15
  • A More Advanced Pie Chart for Analysis Services Data
  • Adobe AIR Programming Unleashed: Working with Windows
  • Performance Testing SQL Server 2008's Change Data Capture Functionality
  • The ABC's of PHP: Introduction to PHP
  • How to Migrate from BasicFiles to SecureFiles Storage
  • Why the Twitter Haters Are Wrong
  • User Personalization with PHP: Beginning the Application
  • Whats in an Oracle Schema?
  • Lighting Enhancement in Photoshop
  •  

    Go Back   WebDeveloper.com > Client-Side Development > JavaScript

    JavaScript JavaScript (not Java) Discussion and technical support, including AJAX and frameworks (JQuery, MooTools, Prototype...)

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1  
    Old 12-20-2006, 05:47 PM
    LukeJea LukeJea is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 16
    replacing text

    Code:
    <HTML>
    <HEAD>
    <TITLE>test</TITLE>
    </HEAD>
    <BODY>
    Replace this: 12345
    </BODY>
    </HTML>
    replace the above body text with
    Code:
    <img src="next.gif">
    Reply With Quote
      #2  
    Old 12-20-2006, 06:05 PM
    scottrickman scottrickman is offline
    Registered User
     
    Join Date: Dec 2006
    Posts: 110
    so what's your question?
    Reply With Quote
      #3  
    Old 12-20-2006, 06:17 PM
    LukeJea LukeJea is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 16
    I want to replace 1234 with

    <img src="next.gif">
    Reply With Quote
      #4  
    Old 12-20-2006, 11:07 PM
    LukeJea LukeJea is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 16
    anyone
    Reply With Quote
      #5  
    Old 12-21-2006, 10:35 AM
    sanjayguha sanjayguha is offline
    Registered User
     
    Join Date: Dec 2006
    Posts: 1
    Try this code if this helps

    See if this logic helps.

    <HTML>
    <HEAD>
    <TITLE>test</TITLE>
    <script language="javascript">
    function callthis()
    {
    inntext = new String(document.body.innerText);
    inntext = inntext.replace("12345","<img src=\"next.gif\">");
    document.write(inntext);

    }
    </script>
    </HEAD>
    <BODY onload="javascript:callthis();">
    Replace this: 12345

    </BODY>
    </HTML>
    Reply With Quote
      #6  
    Old 12-21-2006, 11:36 AM
    Mr J Mr J is offline
    Registered User
     
    Join Date: Apr 2003
    Location: UK
    Posts: 2,203
    innerText is IE only, try this example

    PHP Code:
    <HTML>
    <
    HEAD>
    <
    TITLE>test</TITLE>

    <
    script type="text/javascript">

    onload=function(){

    currentContent=document.body.innerHTML
    newContent
    ='<img src="next.gif">'

    document.body.innerHTML=currentContent.replace(currentContent,newContent)

    }

    </script>

    </HEAD>
    <BODY>

    Replace this: 12345

    </BODY>
    </HTML>
    __________________
    The Silent One

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is one with an idea
    Reply With Quote
      #7  
    Old 12-21-2006, 12:29 PM
    LukeJea LukeJea is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 16
    Wow...it worked..but I still have one question

    The code above only replaces 1 occurence of the string. Is it possible to have the script replace all occurences of the string?
    Reply With Quote
      #8  
    Old 12-21-2006, 01:15 PM
    cyberowl cyberowl is offline
    Registered User
     
    Join Date: Dec 2006
    Posts: 92
    If at least you use some internal tag for the text like

    <span>Replace this: 12345</span>

    you could use a code like this:

    Code:
    <script type="text/javascript">
    
    onload = function() {
    
    	var spanstags = document.getElementsByTagName("span");
    	newContent = '<img src="next.gif">';
    
    	for (var x = 0; x < spanstags.length; x++) {
    		if (spanstags[x].innerHTML == "Replace this: 12345")
    			spanstags[x].innerHTML = newContent;
    	}
    }
    
    </script>
    Reply With Quote
      #9  
    Old 12-21-2006, 03:24 PM
    LukeJea LukeJea is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 16
    Hello, thank you for all your efforts. Here is the code I am using. As you can see I have 4 numbers of "12345". With the code I am using now only replaces the first string and does not touch the other 3. I am trying to make the script replace all 4.

    Code:
    <HTML> 
    <HEAD> 
    <TITLE>test</TITLE> 
    
    <script type="text/javascript"> 
    
         onload=function(){ 
    
            currentContent=document.body.innerHTML 
            newContent='<img src="next.gif">' 
    
           document.body.innerHTML=currentContent.replace("12345",newContent) 
    
         } 
    
    </script> 
    
    </HEAD> 
    <BODY> 
         Replace this: 12345 
         Replace this: 12345 
         Replace this: 12345 
         Replace this: 12345 
    </BODY> 
    </HTML>

    Last edited by LukeJea; 12-21-2006 at 03:28 PM.
    Reply With Quote
      #10  
    Old 12-21-2006, 03:30 PM
    Mr J Mr J is offline
    Registered User
     
    Join Date: Apr 2003
    Location: UK
    Posts: 2,203
    You could also try

    PHP Code:
    <HTML>
    <
    HEAD>
    <
    TITLE>test</TITLE>

    <
    script type="text/javascript">

    onload=function(){

    currentContent=document.body.innerHTML

    newText
    ='<img src="next.gif">'

    oldText = new RegExp("12345", 'gi')

    newContent=currentContent.replace(oldText,newText)

    document.body.innerHTML=newContent


    }

    </script>

    </HEAD>
    <BODY>

    Replace this: 12345 <BR><BR>
    Replace this: 12345 <BR><BR>
    Replace this: 12345 <BR><BR>
    Replace this: 12345 <BR><BR>

    </BODY>
    </HTML>
    __________________
    The Silent One

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is one with an idea

    Last edited by Mr J; 12-21-2006 at 03:36 PM.
    Reply With Quote
      #11  
    Old 12-21-2006, 04:24 PM
    LukeJea LukeJea is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 16
    wow..that worked..Thank you very much..

    May I ask where do you guys get your javascript training from?
    Reply With Quote
      #12  
    Old 12-22-2006, 10:22 AM
    LukeJea LukeJea is offline
    Registered User
     
    Join Date: Sep 2006
    Posts: 16
    OK, this code below works for all the strings that I give it but it does not work for this:
    Code:
    :)
    a smilie. I have read some books on this but cannot find a solution to this problem. If you see the code below the it does not replace
    Code:
    :)
    . I am puzzled!

    Code:
    <HTML> 
    <HEAD> 
    <TITLE>test</TITLE> 
    
    <script type="text/javascript"> 
    
    onload=function(){ 
    
    currentContent=document.body.innerHTML 
    
    newText='<img src="next.gif">' 
    
    oldText = new RegExp(":)", 'gi') 
    
    newContent=currentContent.replace(oldText,newText) 
    
    document.body.innerHTML=newContent 
    
    
    } 
    
    </script> 
    
    </HEAD> 
    <BODY> 
    
    Replace this: 12345 <BR><BR> 
    Replace this: :) <BR><BR> 
    Replace this: 12345 <BR><BR> 
    Replace this: :) <BR><BR> 
    
    </BODY> 
    </HTML>

    Last edited by LukeJea; 12-22-2006 at 10:24 AM.
    Reply With Quote
      #13  
    Old 12-22-2006, 11:50 AM
    Mr J Mr J is offline
    Registered User
     
    Join Date: Apr 2003
    Location: UK
    Posts: 2,203
    Try replacing this line

    oldText = new RegExp(":)", 'gi')

    with this

    oldText = new RegExp(":\\)", 'gi')
    __________________
    The Silent One

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is one with an idea
    Reply With Quote
    Reply

    Bookmarks


    Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
     
    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is Off
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 03:53 PM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy

    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.