admin管理员组文章数量:1278653
I have some search page link: www.example/search.php?search=search_word
, I am tried to make a default search URL. If people only type www.example/search.php
via the browser, make a default URL as www.example/search.php?search=aaa
. My code does not work.
<script src="../jquery.js"></script>
<script>
jQuery(document).ready(function(){
var currneturl = document.URL;
if(!document.URL.indexOf('?')){
document.URL = currneturl + '?search=aaa';
}
});
</script>
I have some search page link: www.example./search.php?search=search_word
, I am tried to make a default search URL. If people only type www.example./search.php
via the browser, make a default URL as www.example./search.php?search=aaa
. My code does not work.
<script src="../jquery.js"></script>
<script>
jQuery(document).ready(function(){
var currneturl = document.URL;
if(!document.URL.indexOf('?')){
document.URL = currneturl + '?search=aaa';
}
});
</script>
Share
Improve this question
edited Jun 22, 2019 at 7:31
double-beep
5,51919 gold badges40 silver badges49 bronze badges
asked Feb 14, 2012 at 10:11
yuli chikayuli chika
9,22120 gold badges78 silver badges123 bronze badges
4
- 2 "My code not work" doesn't describe an observed behaviour and the way that it differs from the desired behaviour – Lightness Races in Orbit Commented Feb 14, 2012 at 10:18
- 2 Why are you doing this with JavaScript instead of PHP? – Quentin Commented Feb 14, 2012 at 10:18
-
@Quentin, how to use
$_SERVER["QUERY_STRING"]
to do this? – yuli chika Commented Feb 14, 2012 at 10:33 -
1
@yulichika — Why would you use that? Test if
$_GET['search']
is set and redirect (or just carry on as normal by using a different value) if it isn't. – Quentin Commented Feb 14, 2012 at 10:34
1 Answer
Reset to default 7The .indexOf()
method returns -1
if the string is not found, and -1
is a truthy value such that !-1
is false
. You need to explicitly test for -1:
if (document.URL.indexOf('?') === -1) {
本文标签: javascriptIf document URL indexOfStack Overflow
版权声明:本文标题:javascript - If document URL indexOf - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741249225a2365480.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论