HTML GET values in Javascript

Reads the values from url, using function name for previous php programmers.

// gerUrlVars from modified to correct some errors in the code.
function $_GET() {
    "use strict";
    var vars = {}, parts = window.location.href.replace(/[?&]+([^=&]+)=([^&#]*)/gi, function (m, key, value) {
        vars[key] = value;
    return vars;

Major change on this version is the regex that is adapted to support Anchor links (hash tags in url).

var value = $;
if (typeof value !== "undefined") { // checks that value has been set, prevents error for trying to read undefined value.

alerts “test”

This entry was posted in JavaScript. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *