View Full Version : 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
djr33
11-26-2007, 01:32 AM
Sessions and/or get variables using PHP are probably both better solutions. Can you link to your page, for a more suited reply?
jscheuer1
11-26-2007, 07:50 AM
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.
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...?
jscheuer1
11-26-2007, 04:07 PM
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.
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
jscheuer1
11-26-2007, 04:40 PM
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.
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'.
jscheuer1
11-26-2007, 07:56 PM
Something like:
<!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.
djr33
11-26-2007, 08:19 PM
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.
Yeh true. The reason I have gone about it with javascript thus far is because it's all I know, I appreciate your offer to help though. :)
Thing is, I am not making the whole website myself, just some of my own frames with frames from another website. This is to make browsing it easier for me, so I kind started a mini-project for myself whilst I learn more basics.
So in basic terms:
I want my search box (in one frame) to open the search (in the other frame). The frame where the search is opened is the website I am browsing, and do not 'own'.
Basically, I am adding the search terms to the search function on the website that already exists via a URL. If the terms return no success in the search of the website, text will appear on the website saying something along the lines of 'Nothing matches the search' - as you might expect. This is the whole 'string' thing I was after. If this occurs, I want my search criteria to search a different URL (hence the search1 and search2 stuff previously) because the first search has been unsuccessful.
Not sure how clearly I explained that... Be honest if it makes no sense whatsoever :P
Much appreciate the help from you both :)
djr33
11-26-2007, 08:52 PM
Oh, so you really do need to parse the page it gets. Interesting.
You could do this serverside with PHP, but it wouldn't be so much easier than JS.
<?php
ob_start();
include('http://....myurl.com/dir/?info=stuff');
$return = ob_get_contents();
ob_end_clean();
if (strpos($return,'My_Word_or_Phrase')!==FALSE) {
die($return);
}
header('Location: http://my.com/error/page.htm');
Since it nicely highlights the code, all you need to do is replace the parts in red.
To make a really smooth system, that may need some tweaking. Rather than outputting the text, it might be a better idea to redirect to that page, as outputting it directly will cause local paths to act strangely.
Also, getting the URL in the first place may be challenging. You can use the variables sent from the form to set the search, such as $_GET['fieldname'], $_POST['fieldname'] based on the method="" attribute of the form.
Errrrrrm... lol i'll take your word for it :P
Sorry, how would I go about implementing this to achieve what I was after with JS?
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.