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?
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?
Share Improve this question asked Jan 3, 2011 at 16:06 BlankmanBlankman 267k332 gold badges795 silver badges1.2k bronze badges 3- define safe? Why wouldnt it be safe to load a url in javascript? – Mark Baijens Commented Jan 3, 2011 at 16:10
- 1 @mark - OP is asking if this is x-browser patible. – Derek Adair Commented Jan 3, 2011 at 16:13
- i recall that some array indexing or something doesn't work in IE but does in firefox. – Blankman Commented Jan 3, 2011 at 17:43
3 Answers
Reset to default 3You 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' );
}
<script language="javascript" type="text/javascript">
var s="yourstring";
if(document.location.href.search(s) != -1)
document.write("yes");
else
document.write("no");
</script>
?
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 pose new ones.
http://projects.allmarkedup./jquery_url_parser/