admin管理员组文章数量:1335609
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/
本文标签: jqueryGet current url using javascriptand see if string existsStack Overflow
版权声明:本文标题:jquery - Get current url using javascript, and see if string exists - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742392829a2466328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论