Reputation: 9221
I have some search page link: www.example.com/search.php?search=search_word
, I am tried to make a default search URL. If people only type www.example.com/search.php
via the browser, make a default URL as www.example.com/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>
Upvotes: 2
Views: 22860
Reputation: 150070
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) {
Upvotes: 7