admin管理员组文章数量:1278975
How to make a window.location.href
judge?
if window.location.href
not match ?search=
, make the current url jump to http://localhost/search?search=car
my code not work, or I should use indexOf
to make a judge? Thanks.
if(!window.location.href.match('?search='){
window.location.href = 'http://localhost/search?search=car';
}
How to make a window.location.href
judge?
if window.location.href
not match ?search=
, make the current url jump to http://localhost/search?search=car
my code not work, or I should use indexOf
to make a judge? Thanks.
if(!window.location.href.match('?search='){
window.location.href = 'http://localhost/search?search=car';
}
Share
Improve this question
asked Nov 7, 2011 at 19:10
fish manfish man
2,72021 gold badges55 silver badges94 bronze badges
0
1 Answer
Reset to default 8A couple of things: you're missing a closing paren, and you need to escape the ? because it's significant to regular expressions. Use either /\?search=/
or '\\?search='
.
// Create a regular expression with a string, so the backslash needs to be
// escaped as well.
if (!window.location.href.match('\\?search=')) {
window.location.href = 'http://localhost/search?search=car';
}
or
// Create a regular expression with the /.../ construct, so the backslash
// does not need to be escaped.
if (!window.location.href.match(/\?search=/)) {
window.location.href = 'http://localhost/search?search=car';
}
本文标签: javascriptjs if windowlocationhref not matchthen jump toStack Overflow
版权声明:本文标题:javascript - js if window.location.href not match, then jump to - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741264058a2368138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论