0

I want do something: if something type url address like: www.mydomain.com/search(without request part), I want do something with javascript, fill with the url like www.mydomain.com/search?search=car, but nothing happened in my code.

   <script>
    var curent_url = document.URL;
    if(document.URL.indexOf("?") < 0){ //if(!document.URL.match(/\?/)){
        //alert('ok');
        document.URL = curent_url+'?search=car';
    }
    </script>

1 Answer 1

2

Instead of document.URL, check for an empty document.location.search:

if (document.location.search.length === 0) {
  // empty query string... append your part to doc
  document.location.href = document.location.href + "?search=car";
}

Examine the document.location object in your console to see what else it offers. There's usually no need to parse out document.URL or document.location.href directly.

console.dir(document.location);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.