www.webdeveloper.com
Results 1 to 6 of 6

Thread: [RESOLVED] IF textstring appears, then write text...

  1. #1
    Join Date
    Apr 2009
    Posts
    3

    resolved [RESOLVED] IF textstring appears, then write text...

    So I'm trying to do what could be described as something obscenely simple, but I can't seem to get the right resources or throw together the right code.

    Here's the long version: I'm writing code for a website, but it's all in form-fields. It's like a really basic version of SharePoint. It uses ASP and JavaScript, and the only customization is through filling out forms which luckily allow code.

    The site requires users to log in before they can access certain things, and if the user is not logged in, the text "Become a User" appears on the page. It's all determined server-side whether or not the user is logged in, so there's no JavaScript variable I can use. So I'm trying to make it simple - if the text string "Become a User" appears anywhere on the page, then I'd like to display the text "Click on Become a User to sign up!" or something similar. (Reason for this is "Become a User" is very small and easy to miss) I've forwarded my request to the site administrators, but they're woefully incapable of helping.

    This is what I came up with, but it seems to display the "Click on Become a User" portion every time, even if the text doesn't appear.

    Code:
    <script type="text/javascript">
    if (/Become a User/.test(document.body.innerHTML)) { document.write('Click on Become a User!'); }
    else { document.write(' '); }
    </script>
    Hopefully that's a good-enough explanation of my situation. I've thrown this into a basic document and test it in IE7, but even if I change the text string to test for to "hamburger," it still displays.

  2. #2
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    if the text string "Become a User" appears anywhere on the page
    Are you sure that string is not always in the html? They could just be hiding/showing the element containing it via css display or visibility property. And if you determine such to be the case, you could, after the page has loaded, check that elements display/visibility. Another thought, have you checked if there's anything they set in a cookie that might indicate if the user is a member?
    ....test(document.body.innerHTML)) { document.write(....
    You might not want to test the body innerHTML until the page has loaded completely either, and in that case, document.write should not be used after the page has loaded. Although, this might be o.k. the way it is, if this script-let appears below the desired text in the page, not sure.. still unsure the body's innerHTML is actually available yet.

  3. #3
    Join Date
    Apr 2009
    Posts
    3
    Quote Originally Posted by astupidname View Post
    Are you sure that string is not always in the html? They could just be hiding/showing the element containing it via css display or visibility property. And if you determine such to be the case, you could, after the page has loaded, check that elements display/visibility. Another thought, have you checked if there's anything they set in a cookie that might indicate if the user is a member?
    Checking the source in IE on the page when I'm logged in shows that "Become a User" does not appear anywhere in the source. When I am not logged in and access the page, it does appear (obviously). There is a CSS class for the table cell in which the text appears, and also a class for the hyperlink, both of which are not used anywhere else in the source code. Did a search for "display" and found one unrelated "display:none" which appeared in both instances of source code.
    Quote Originally Posted by astupidname View Post
    You might not want to test the body innerHTML until the page has loaded completely either, and in that case, document.write should not be used after the page has loaded. Although, this might be o.k. the way it is, if this script-let appears below the desired text in the page, not sure.. still unsure the body's innerHTML is actually available yet.
    This particular script-let does appear below the "Become a User" text.

    No matter what I use as the text string to test, it will still display the first document.write function rather than write a blank space. I just tested for "Mephistofeles" and it still displayed "Click on Become a User." Makes me think there's something wrong with my code, or that this just isn't possible.
    Last edited by CMK; 04-21-2009 at 08:02 AM.

  4. #4
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726

    The Cure For Self-Testing

    Ahhh.. you are correct, it doesn't work that way, no matter what. This is one of those silly little riddles that, when you figure it out, you kinda go.. ohhh, geez that was simple....
    The problem is that you are placing the string to test for into the html yourself (within the script-let's code), so using .test() will not be what you want to do. The .match() method returns an array of matches if a match/matches are found (need to be sure and use the "g" flag for global match in this case, else it will just match one time). See example:


    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
    <title>Some Title</title>
    </head>
    <body>
    <div>
    It will count this occurence of Become a User<br>
    <script type="text/javascript">
    var html = document.body.innerHTML;
    var rex = new RegExp("Become a User","g"); //It counts this occurence of the string, and 
    //in this case, using the new RegExp constructor is necessary, with the "g" flag for global
    var txtMatch = html.match(rex);
    if (txtMatch !== null && txtMatch[2]) { //[2], for 3 items in the array, means it appears outside of this script-let also
    alert(txtMatch + "\n"+ txtMatch.length); 
    
    document.write('Click on Become a User!'); //it appears here, is detecting it here also, but won't detect it below here
    }
    </script>
    </div>
    </body>
    </html>

  5. #5
    Join Date
    Oct 2008
    Location
    U.S.
    Posts
    726
    Are you sure that string is not always in the html?
    Turns out I was right there!!

  6. #6
    Join Date
    Apr 2009
    Posts
    3

    Thumbs up

    Brilliant! That's done it perfectly. I can't thank you enough.
    Quote Originally Posted by astupidname View Post
    Turns out I was right there!!
    I'd suspected that may have been the case.

    It's kind of like that childish little word-game where someone says, "Don't do anything until I say 'go.' "

    "You just said 'go!' I'm going!"

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles