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 8 of 8

Thread: Text exists

  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Text exists

    Is there any way to check if a text is exists in the current page or not ?

    For example I have a paragraph in my template like that

    Code:
    <p>coding forum</p>
    I want to check if (coding forum) is existing in the current page and if not exist return an alert code.

    Any ideas ? I can check for div ids however I don't know what about the strings.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,099
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    See if this meets your needs.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <meta charset="utf-8" />
    <title> Untitled </title>
    <script type="text/javascript">
    function searchForInfo() {
      var info = new RegExp(document.getElementById('searchInfo').value); 
      var content = document.getElementById('bodyContent').innerHTML;
      alert(info.test(content));
    }
    </script>
    
    <style type="text/css">
    
    </style>
    </head>
    <body id="bodyContent">
    <button onclick="searchForInfo()">Search for</button>
    <input type="text" id="searchInfo" value="coding forum">
    <p>
    
    This is a simple page with some sample text.
    <p>coding forum<p>
    More information continues on the page.
    
    </body>
    </html>

  • #3
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are close to what I need, I don't want something like a form.

    I want it pre-defined by me not by the visitor via a button, So I want to make var info always equals to xxx and it will check the body content by id and onclick if it not found the string it should appear an alert.

    You are so close just the above point and it will be the same like what I want

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    this may or may not be what you're looking for, but it examines all the text nodes in the body (so ignores id's and the like) for the term "coding forum" except for occurrences within the script tags:

    Code:
    <script type="text/javascript">
    var found=false;
    function recurse(element,str) {
        if (element.childNodes.length > 0) {
            for (var i = 0; i < element.childNodes.length; i++) {
                recurse(element.childNodes[i],str);
    				}
    			}
    if (element.nodeType == 3 && element.nodeValue.match(str) && element.parentNode.tagName!="SCRIPT") {
            found=true;
    		return;
    		}	
    }
    
    recurse(document.body, "coding forum");
    
    if (!found) {
    	alert("not found");
    }
    </script>

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,099
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question

    Quote Originally Posted by 2CODE View Post
    You are close to what I need, I don't want something like a form.

    I want it pre-defined by me not by the visitor via a button, So I want to make var info always equals to xxx and it will check the body content by id and onclick if it not found the string it should appear an alert.

    You are so close just the above point and it will be the same like what I want
    Second paragraph above does not make sense to me.
    How is this script going to be used?
    If the info always equals to xxx, then why the search? You would know if you typed it into the script or not.
    Also, what onclick event is to occur if the xxx is not found?

    Not very clear to me what it is that you are trying to accomplish.

  • #6
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    this may or may not be what you're looking for, but it examines all the text nodes in the body (so ignores id's and the like) for the term "coding forum" except for occurrences within the script tags:

    Code:
    <script type="text/javascript">
    var found=false;
    function recurse(element,str) {
        if (element.childNodes.length > 0) {
            for (var i = 0; i < element.childNodes.length; i++) {
                recurse(element.childNodes[i],str);
    				}
    			}
    if (element.nodeType == 3 && element.nodeValue.match(str) && element.parentNode.tagName!="SCRIPT") {
            found=true;
    		return;
    		}	
    }
    
    recurse(document.body, "coding forum");
    
    if (!found) {
    	alert("not found");
    }
    </script>
    Doesn't make any effect in the page whether if there's coding forum or not.

  • #7
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,983
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I suspect it's time for you to show some code so we can see what you are trying to do, but...
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    
    <html>
    <head>
    <title>District</title>
    
    <body>
    
    <div id="coding forum">blah blah blah codin forum blah blah blah </div>
    <script type="text/javascript">
    var found=false;
    function recurse(element,str) {
        if (element.childNodes.length > 0) {
            for (var i = 0; i < element.childNodes.length; i++) {
                recurse(element.childNodes[i],str);
    				}
    			}
    if (element.nodeType == 3 && element.nodeValue.match(str) && element.parentNode.tagName!="SCRIPT") {
            found=true;
    		return;
    		}	
    }
    
    recurse(document.body, "coding forum");
    
    if (!found) {
    	alert("not found");
    }
    </script>
    </body>
    </html>
    feel free to add the "g" to see the difference.

  • #8
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's worked thank you!


  •  

    Posting Permissions

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