0

I need a reliable way to get the current URL in the browser using javascript, and to test whether a string is contained in the url.

Is this a safe process between all the browsers?

3
  • define safe? Why wouldnt it be safe to load a url in javascript? Commented Jan 3, 2011 at 16:10
  • 1
    @mark - OP is asking if this is x-browser compatible. Commented Jan 3, 2011 at 16:13
  • i recall that some array indexing or something doesn't work in IE but does in firefox. Commented Jan 3, 2011 at 17:43

3 Answers 3

3

You could use .indexOf() against the window.location.href, but be aware that you're testing the entire url.

if( window.location.href.indexOf( 'someString' ) != -1 ) {
    alert( 'the string was found' );
}
Sign up to request clarification or add additional context in comments.

1 Comment

@Yads: That is simply not true. Perhaps you're thinking of Array.indexOf(). This is String.indexOf().
2
<script language="javascript" type="text/javascript"> 
   var s="yourstring";
   if(document.location.href.search(s) != -1)
        document.write("yes");
   else 
        document.write("no");
</script>

?

Comments

2

I really like the jQuery URL Parser to work against the URL in JavaScript. It is a great utility to get various parts of the URL and compose new ones.

http://projects.allmarkedup.com/jquery_url_parser/

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.