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
Add a ment  | 

1 Answer 1

Reset to default 7

The .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