Is there a way to get query string values of a URL in JavaScript? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Is there a way to get query string values of a URL in JavaScript?

Damian99Damian99 USAPosts: 116Member

Can someone please let me know whether there is a way to get query string values of a URL in JavaScript?

Ex. www.someurl.com/?name=john&age=25&country=US

Tagged:

Comments

  • DavidMDavidM USAPosts: 342Member

    Use the VanillaJS library as follows.

    function getParameterValueByName(name) {
        name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)");
        var results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    
Sign In or Register to comment.