0

html code

function getQuerystring() {
  var getUrlParameter = function getUrlParameter(sParam) {
    var sPageURL = window.location.search.substring(1),
      sURLVariables = sPageURL.split('&'),
      sParameterName,
      i;

    for (i = 0; i < sURLVariables.length; i++) {
      sParameterName = sURLVariables[i].split('=');

      if (sParameterName[0] === sParam) {
        return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
      }
    }
  };

  var blog = getUrlParameter('c');
  document.getElementById('detail').innerHTML = blog;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="border: dashed; size: auto;"><a name="divtest" href="#detail?c=active" id="test" onclick="getQuerystring()">testing</a>
</div>
<div id="detail" style="border: 2px; size: auto;"></div>

this function should display the value of c in the link but instead it displays undefined in the div.i have searched a lot and tried so many of things but nothing seems to be working. kindly guide me the right code i will be thankful to you.

James Z
  • 12,209
  • 10
  • 24
  • 44
Coder
  • 7
  • 3
  • Your code works fine. Not in the snippet, because it uses `location.search` which doesn't contain `c=` but if you set `var sPageURL = "c=active";` then it works fine - so the assumption is that your window.location doesn't contain `c=`. – freedomn-m Nov 27 '19 at 17:11
  • Your *link* does contain `c=` and your comment is "should display the value of c in the link" - but your code doesn't attempt to get anything from the link. – freedomn-m Nov 27 '19 at 17:12
  • plz suggest me through code – Coder Nov 27 '19 at 17:13
  • [This](https://stackoverflow.com/questions/19491336/get-url-parameter-jquery-or-how-to-get-query-string-values-in-js) might help you. – hbamithkumara Nov 27 '19 at 17:14
  • i get help from this code. as you see it is the same code. but still gives undefined. – Coder Nov 27 '19 at 17:16

3 Answers3

0

window.location.search will:

  • Give you data from the URL of the current page, not the page being linked to
  • Give you data from the query string (which starts with ? and goes on until the start of the fragment), not the fragment (which starts with # and goes on until the end … even if the fragment includes a ?)

You would need to use refToTheAnchor.search and fix the order of your query and fragment in the attribute.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Get parameter From Link

Your code uses

var sPageURL = window.location.search.substring(1)

which is not the "from the link", so needs to be changed. As you've tagged jquery (despite not having any jquery), here's one possible solution:

$("#test").click(function() {
  getQuerystring(this);
  return false;
});

function getQuerystring(el) {
  console.log(el.href);
  var getUrlParameter = function(sParam) {
    var sPageURL = el.href.split('?')[1],
      sURLVariables = sPageURL.split('&'),
      sParameterName;
      
    for (var i = 0; i < sURLVariables.length; i++) {
      sParameterName = sURLVariables[i].split('=');

      if (sParameterName[0] === sParam) {
        return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
      }
    }
  };

  var blog = getUrlParameter('c');
  document.getElementById('detail').innerHTML = blog;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div style="border: dashed; size: auto;">
  <a name="divtest" href="#detail?c=active" id="test">testing</a>
</div>
<div id="detail" style="border: 2px; size: auto;"></div>
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • kindly guide me how to pass the value from javascript to php on same page. m trying using cookie i-e "document.cookie = "blog = " + blog;" and my php code is $chk = $_COOKIE['blog'];. but it isnt working – Coder Nov 27 '19 at 17:56
  • 1
    Please ask a new question if you have a new question. – freedomn-m Nov 28 '19 at 08:57
0

You could try using this function which uses RegEx to find a query parameter. Pass the Parameter name to it to return the value.

//to get query parameters.
$.urlParam = function (name) {
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec($(location).attr('href'));
    return (results) ? results[1] : "";
}

And then you can call this method in your code like below.

var paramValue = $.urlParam('paramName');

You could modify the method to pass any url if you dont want to use the location.href value.

Nidhi Gupta
  • 101
  • 1
  • 4