Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: If text string is present, do [whatever]...?

  1. #1
    Join Date
    Feb 2006
    Location
    Harpenden, Hertfordshire, England
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default If text string is present, do [whatever]...?

    Hi I'm looking for a script that recognises certain text on a page, with the effect of:

    if (text is on page) {
    goto page1;
    }

    else {
    go page2;
    }

    The page url is always the same, and I think the only way for some script to know whether I am on page1 or page2, is through a small string of text which is only on page1.

    Seems pretty simple but I really don't know where to start with only a basic understanding, and Google isn't turning up anything that I understand and could adapt...

    Thanks in advance

    Chris

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Sessions and/or get variables using PHP are probably both better solutions. Can you link to your page, for a more suited reply?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In the broadest of terms, one could probably do:

    if (/string to test for/.test(document.body.innerHTML))
    do whatever;
    else
    do some other thing;

    But it would require that the user has javascript enabled, not to mention trying it out to make sure it works.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Feb 2006
    Location
    Harpenden, Hertfordshire, England
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for the replies, I will outline what I have thus far...

    (FYI, 'query' is the 'id' given to the form used for this. Have just used red formatting to highlight the differences.)

    function goToUrl(form) {
    query = form.query.value;

    location = "http://www.website.com/page.php?search1="+query+"";

    if (/string to test for/.test(document.body.innerHTML)) {
    location = "http://www.website.com/page.php?search2="+query+"";
    }

    }

    ... and then in the 'form' tag, the 'onsubmit' tag calls this function.

    Perhaps my 'if' argument is out-of-place here are should be in a separate function...? I feel this because the string it looks for will be on the search1 page, so the string may not return as being present 'onsubmit' of the form.

    I hope it is clear what I was to achieve :S
    Basically:
    - do the search through search1;
    - if the string returns as being present on the search1 page, do the search on page2 instead.

    I am sure there is a better, easier, more effective way of doing this using php, or what not, but I am just starting out and experimenting with javascripting so please forgive my ignorance djr33.

    Thanks a lot,
    Chris

    [edit]

    I am assuming that the '.test(document.body.innerHTML)' is a pretty standard function, but can be modified accordingly. For example:

    .test(parent.frame-name1.frame-name2.body.innerHTML)'

    would still work assuming I have a page with a frames, which then may have a frame within one of these frames...?
    Last edited by c24w; 11-26-2007 at 03:53 PM.

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Yes, but is it working? The test() method is standard javascript, but its usage is a little different than many others. It starts with a regular expression. That's followed by a dot, the word test, and then a parenthetical string to be tested against the regular expression. You can look up regular expressions and the javascript test method on the web. A regular expression may be a literal, as long as any special characters in it that are meant to be literals are escaped.

    So, as an example:

    /boy/.test('boys and girls')

    would evaluate as true,

    /boy/.test('fish in the sea')

    false.

    Both the string to be tested and the regular expressions may come from variables, but the RegExp can be trickier to set than an ordinary variable. It is easiest to write it directly into the method.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #6
    Join Date
    Feb 2006
    Location
    Harpenden, Hertfordshire, England
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    OK, I understand the concept but still can't get it working :S. I'm sure this should be simple... Given the scenario (searching for a string on the page using javascript), would you go about scripting it the way I have? I appreciate any suggestions you may have.

    Also, I now presume that the forward slashes around the string have a purpose, and were not just there originally to denote that I should change it...? Or am I being stupid? :P

    'document.body.innerHTML' appears that it would search the page HTML for the string. So within my 'if' argument, if it returns as 'true' on page1, it should then do the search on page2, in theory... Ah well, it all seems logical, I will keep experimenting with what I have.

    Thanks very much for the short lesson
    Chris

  7. #7
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Where is the string that you are testing for? If it is in an input in the form, put there by the user, innerHTML probably won't find it.

    I think you need to let go of any particular method for the moment, and explain exactly what it is the user does, and may do, and what you would like to have happen as a result of the various possible actions taken on the user's part.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  8. #8
    Join Date
    Feb 2006
    Location
    Harpenden, Hertfordshire, England
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Sure, I should have gone into more detail previously.
    What I want is the following:

    Basically a page with frames, what I want to achieve is using 2 frames - one with the search input in it; on with the search results. For the purpose of this, I will refer to the frames as 'search' and 'main'.

    1. User inputs search term in input field and clicks search (search frame) - forming my 'query'.

    2. Search term ('query') is searched for via:
    parent.main.location = ".../page.php?search1='+query+'"
    as above... (therefore in main frame)

    3. Now, I would like to detect whether a certain string is present in the 'main' frame, and if it is there, search my 'query' via search2 (not search1)... using:
    if (/my_string/.test(parent.main.body.innerHTML)) {
    parent.main.location = ".../page.php?search2='+query+'"
    }

    Therefore with the effect of:
    - searching for a term ('query') in the 'search' frame.
    - loading the search 'query' in the 'main' frame, via search1.
    - if a certain string is now present in the 'main' frame, search via search2 instead. If not (i.e. 'else') return false and do nowt

    I realise that it is difficult for you to understand what I'm hoping to achieve, and I have probably repeated myself numerous times above... :S

    Cheers

    [edit]

    To answer your first question simply...
    The string I am testing for is in the 'main' frame, just in the HTML body, and is created as a result of certain search criteria.

    For a really random example, one search turns up a string of 'you searched for cheese';
    Another, 'you searched for fish';

    My intention would be to detect certain words from what is created here, such as 'cheese'.
    Last edited by c24w; 11-26-2007 at 05:57 PM.

  9. #9
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Something like:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script type="text/javascript">
    onload=function(){
    var l=window.location, s=l.search;
    if (/cheese/.test(document.body.innerHTML)&&!/search2=/.test(s)&&/search1=/.test(s))
    l.href=l.href.replace(/\?search1=/, '?search2=');
    }
    </script>
    </head>
    <body>
    <div>cheese</div>
    </body>
    </html>
    However, it would be better to intercept the data that writes 'cheese' or whatever to the page and have the server serve the search2 page instead. That way even non-javascript enabled users will get the proper search.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  10. #10
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    If you describe the situation in general terms (ie, not related to Javascript), I'd be more than happy to help you figure out a PHP solution. Searching the whole page, especially with Javascript, is a very roundabout and processor intensive way of doing this.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

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
  •