0

I'm trying to get a simple example of using js to retrieve querystring params working and it's not (even though I said simple). Here's my code:

I've tried putting alert statements in and debugging the old fashioned way but to be honest I'm used to using VS2017 for c# not js

Here's the code I'm using.

I have 2 html pages, the first just has a link:

<a href="h1.html?type=generalk">try me</a>

The second has the code:

    this is some text <br />
    <script> getparams2();</script>   

    this is some more text <br />   


    <script>
        function getUrlParam(parameter, defaultvalue) {
            var urlparameter = defaultvalue;
            if (window.location.href.indexOf(parameter) > -1) {
                urlparameter = getUrlVars()[parameter];
            }
            return urlparameter;
        }
    </script>

    <script>
        function getUrlVars() {
            var vars = {};
            var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
                alert(value);
                vars[key] = value;
            });
            return vars;
        }
    </script>

    <script>
        function getparams2()
        {

            var mytext = getUrlVars()["type"];

        }
    </script>

The result I'm trying to acheive is that the h1.html page can display the type parameter from the url.

Thanks in advance, Paul.

paul33
  • 15
  • 3

2 Answers2

0

I would do it the following way:

URL-Param function

function getUrlParam(name){
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null) { return ""; }
  else { return results[1]; }
};

Call from your getParams2

function getParams2(){
  return getUrlParam('type');
};

getParams2 will return the value of the param if it is in the URL.

Okeano
  • 108
  • 1
  • 8
0

Your code works. You just need to execute it when the document is ready also you need to return something or update your page to see the results.

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
</head>
<body>
<div id="params"> Printing The URL Params here</div>

<script type="text/javascript">
$( document ).ready(function() {
  getparams2();

  function getUrlParam(parameter, defaultvalue) {
      var urlparameter = defaultvalue;
      if (window.location.href.indexOf(parameter) > -1) {
          urlparameter = getUrlVars()[parameter];
      }
      return urlparameter;
  }

  function getUrlVars() {
      var vars = {};
      var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function 
(m, key, value) {
          alert(value);
          vars[key] = value;
      });
      return vars;
  }

  function getparams2()
  {
      var mytext = getUrlVars()["type"];
      //console.log(mytext);
      $('#params').text(mytext);
  }

});

</script>
</body>
</html>
Aness
  • 610
  • 1
  • 7
  • 24