简体   繁体   中英

Add parameters on href url with javascript and with specific conditions

I have some problem with my javascript functions. I want to add some parameters to the href url when it cliked. For example I want to add ?param=1 in href url when it's clicked. But before add some parameters I want to check if the url in href is http://example.com and that parameters not contain ?param=1 . After it checked and the url is http://example.com and not contain ?param=1 , I want to add the ?param=1 in that url. Why I want to check first before adding the parameters? It's because I don't want to add the parameters in any url but just in specific url which I want.

This is what I do till now:

window.addEventListener("click", function(e) {
    var href = e.target.getAttribute("href");
    if(href) {
        location.href = href + "?param=1";
        e.preventDefault();
    }
});​

But, I don't know how to check and make specific conditions if the href is http://example.com . Please anybody know how to do this could help me. Thanks before.

Check the queryString and see if it has the param1 value, if it does not then append it.

var url = new URL("http://www.example.com?param=1"); //window.location.href

if (url.searchParams.get('param'))
{
  console.log("Param exists");
}
else
{
   console.log("Param does not exist")
   url + "?param=1"
   console.log(url);
}

 window.addEventListener("click", function(e) { e.preventDefault(); var href = e.target.getAttribute("href"); console.log(href); if(href) { if(href.indexOf("param=1")==-1){ href = href + "?param=1"; console.log(href); // location.href = href; }else{ href = href + "?param=2"; console.log(href); // location.href = href; } // } }); 
 <a href="http:example.com?param=1">click</a> 

The index to start the search in the string or object. It will return -1 if there is no search found.

 $(document).ready(function(){ var href = "www.example.com?param=1"; if(href.indexOf("www.example.com") >=0 && href.indexOf("param=1") >= 0) { alert('Url has given words'); //location.href = href + "?param=1"; // e.preventDefault(); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>Check URL</div> 

Check it out:

 window.addEventListener("click", function(e) { const href = e.target.getAttribute("href"); if(href) { console.log(href.includes('?param=1')); // You can do whatever you want based on this condition e.preventDefault(); } }); 
 <a href='http://example.com/'>Link 1</a> <br/><br/> <a href='http://example.com/?param=1'>Link 2</a> 

Learn about indexOf for check if values exist in the querystring

 var str="Hello world!" document.write(str.indexOf("Hello") + "<br />") document.write(str.indexOf("World") + "<br />") document.write(str.indexOf("world")) 

  • The indexOf () method is sensitive to case sensitive.
  • If the string value that is to be retrieved does not appear, the method returns -1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM