0

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.

Antonio
  • 755
  • 4
  • 14
  • 35
  • You can use this logic to check if values exist in the querystring: https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – Rory McCrossan Oct 08 '18 at 07:44

5 Answers5

0

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);
}
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

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.

Sumesh TG
  • 2,557
  • 2
  • 15
  • 29
  • @Antonio window.location.href returns the href (URL) of the current page window.location.hostname returns the domain name of the web host window.location.pathname returns the path and filename of the current page window.location.protocol returns the web protocol used (http: or https:) window.location.assign loads a new document – Sumesh TG Oct 08 '18 at 08:06
  • @Antonio You can simply split the string using `?` symbol `var domain=url.split('?')[0];` – Sumesh TG Oct 08 '18 at 08:10
  • It works. But if I delete `e.preventDefault();` why it open the link without `?param` ? – Antonio Oct 08 '18 at 08:16
  • @Antonio It will prevent the default behavior click on ` – Sumesh TG Oct 08 '18 at 08:43
0

$(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>
SilentCoder
  • 1,970
  • 1
  • 16
  • 21
0

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>
Humayun Kabir
  • 518
  • 4
  • 15
-1

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