3

Is it possible to check if a url address starts with for example: http://www.odsavacky.cz/blog/wpcproduct/ ?

0

3 Answers 3

6

Just use normal string searching:

// if the url begins with this string
if (window.location.href.indexOf('http://www.odsavacky.cz/blog/wpcproduct/') == 0) {
    // Do what you want
}
Sign up to request clarification or add additional context in comments.

6 Comments

I don't get the indexOf() part. Can't you do if (window.location.href == 'http://www.odsavacky.cz/blog/wpcproduct/')?
That checks if the whole URL exactly matches, not just the beginning.
Cool, got it! Was overreading the term start in the question anyway. ;)
should be === -1, not == 0
@CătălinRădoi That tests if it doesn't contain the string.
|
3
if (url.startsWith("http://google.com/")) {
     // do something
}

Comments

0

No need for regular expressions. You can just do it like this

str="http://www.odsavacky.cz/blog/wpcproduct/blah"
function search(string){
    return (string.indexOf(str)==0)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.