/    Sign up×
Community /Pin to ProfileBookmark

Search strings

Hi Im new to the forums. Here is what Im trying to do. I am grabbing a url, converting it to a string and searching if that string contains the word “true”.
If it finds true i set the page to refresh every 5 seconds. If true isn’t there the page will refresh every 6 minutes. Currently it refresh every 5 seconds no matter if it finds “true” in the string or not.

var url = document.location.href;

var pat = /True/i;
url = String(url);
var word = url.search(pat);
if (word = true)
{
setTimeout(“location.reload()”, 5000);
}
else
{
setTimeout(“location.reload()”, 6000000);
</script>

i think what im doing wrong is that the pattern search doesnt return the value “true” but a number. But Im not sure.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineSep 03.2010 — String.search returns the index of the found string or -1 on failure.

Use:[CODE]setTimeout( 'location.reload()', location.href.search(/true/i) > -1 ? 5000 : 360000 );[/CODE]

You could refine the search but you don't say which part of the URL should contain the string. Is it a parameter?
Copy linkTweet thisAlerts:
@AlBarauthorSep 03.2010 — Thanks for the reply.I 'm passing a parameter from the previous page whether a checkbox is checked or not. I just need to search the string. If it has the word true in it i want it to refresh every 5 seconds. If the word is false or any other word, I want it to refresh every 6 minutes.

I have also tried this: but I dont know how to interact with the retrun value from the test() method.

var url = document.location.href;

var pat = /True/i;
var word = pat.test(url);
alert(url);
if (word = true)
{
setTimeout("location.reload()", 5000);
}
Copy linkTweet thisAlerts:
@Sterling_IsfineSep 03.2010 — You used the wrong operator:
[CODE] var url = document.location.href;

var pat = /True/i;

var word = pat.test(url);
if (word [B][COLOR="RoyalBlue"]==[/COLOR][/B] true)
{
setTimeout("location.reload()", 5000);
}[/CODE]


Something a bit stricter, say the parameter is called 'checked':[CODE]setTimeout( 'location.reload()', location.href.search(/(?|&)checked=true(&|$)/i) ? 5000 : 360000 );[/CODE]
×

Success!

Help @AlBar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...