Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery read query string

Tags:

jquery

I'm having trouble reading a query string with Jquery. What I want to do is read the query string and based on what it says scroll to a certain place or element.

here is my code

    $(document).ready(function () {
        var name = getQueryParam("id")
         { var pairs = location.search.substring(1).split('&'); for (var i = 0; i < pairs.length; i++) 
         { var params = pairs[i].split('='); if (params[0] == param) { return params[1] || ''; } } return undefined; }; })(jQuery);

        if ( name == 1){
        scrollTo(0, 800);
        }
    });
like image 271
poerg Avatar asked Nov 28 '22 15:11

poerg


1 Answers

You can use this function to get query string value:

function getParameterByName( 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 decodeURIComponent(results[1].replace(/\+/g, " "));
}

Example:

var param = getParameterByName('yourVar');
like image 76
Sarfraz Avatar answered Dec 09 '22 20:12

Sarfraz