Comments on: BUJS #1 – getParameterByName https://j11y.io/javascript/bujs-1-getparameterbyname/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Ifthikhan Nazeem https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2049 Tue, 01 Nov 2011 09:12:15 +0000 https://j11y.io/?p=1744#comment-2049 @Felix Your code will have issue if the value of a key is a base64 encoded string.

]]>
By: Todd Wheeler https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2048 Sun, 11 Sep 2011 23:06:48 +0000 https://j11y.io/?p=1744#comment-2048 @James,

I wanted to pass a link through the query string,… but think instead that it would make more sense to parse through the query on the receiving page to create a link.

What I’m trying to do is to send the name of an image.jpg through the query string, and then provide a link to a hi-res version of the image on the receiving page.

]]>
By: Álvaro G. Vicario https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2047 Mon, 11 Jul 2011 06:12:41 +0000 https://j11y.io/?p=1744#comment-2047 @Bush Tag: It’s pretty normal that JavaScript code won’t pass HTML validation, isn’t it?

]]>
By: Bush_Tag https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2046 Tue, 07 Jun 2011 09:35:14 +0000 https://j11y.io/?p=1744#comment-2046 Iam getting these errors in w3c validation in the “&” plz can anybody help

Line 166, Column 27: xmlParseEntityRef: no name

var match = RegExp(‘[?&]’ + name + ‘=([^&]*)’)

]]>
By: James https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2045 Thu, 03 Mar 2011 07:55:00 +0000 https://j11y.io/?p=1744#comment-2045 @Felix, Andy E. mentioned that idea and FWIW, if we were talking about potential applications of the function I would agree, but the purpose of BUJS is simply to take ugly code and make it prettier and more efficient — even if it is fighting the wrong battle.

@Alvaro, I agree–it should be supported natively.

@Joss, thanks! 🙂

]]>
By: Joss Crowcroft https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2044 Thu, 03 Mar 2011 04:08:30 +0000 https://j11y.io/?p=1744#comment-2044 Love the article dude – you could also have picked up on the three var ; declarations right after each other that I guess could have been combined!

I wouldn’t have spotted those crazy optimisations though. As soon as I see regexes my eyelids feel really heavy.

]]>
By: Felix https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2043 Wed, 02 Mar 2011 15:36:28 +0000 https://j11y.io/?p=1744#comment-2043 I’m usally generating an JS-object out of the parameters. That makes many things a whole lot easier – and prevents me from using RegExps. Besides, if you plan to look up multiple parameters, my solution will perform better. Just to illustrate the main idea (the code hasn’t been tested):

var querystring = (function(query){
    var qs = {},
        params = query.split("&"),
        parts;
    for(var i = 0; i < params.length; i++){
        parts = params[i].split("=");
        qs[ parts[0] ] = parts[1] ? parts[1] : "";
    }
 
    return qs;
})(document.location.search ? document.location.search.substring(1) : "");
]]>
By: Álvaro G. Vicario https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2042 Wed, 02 Mar 2011 15:25:26 +0000 https://j11y.io/?p=1744#comment-2042 @James – Nice bit, thank you. I’ve always wondered why JavaScript lacks a native RexExp method to quote a literal.

]]>
By: Andy E https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2041 Wed, 02 Mar 2011 00:07:52 +0000 https://j11y.io/?p=1744#comment-2041 @James,

That’s also a very nice workaround. Looking forward to BUJS #2 – maybe I’ll join in if I come across some horrendously ugly code 🙂

]]>
By: James https://j11y.io/javascript/bujs-1-getparameterbyname/#comment-2040 Tue, 01 Mar 2011 23:50:53 +0000 https://j11y.io/?p=1744#comment-2040 @Andy, I agree 100%. To be honest, when I did this I wasn’t thinking so much about application but simply about code clarity/readability and micro-efficiency.

@Everyone, if you care, and want to use this for your app, please use Andy’s answer or at least add some caching in, e.g.

var getParameterByName = (function(){
 
    var c = {};
 
    return function getParameterByName(name) {
 
        name = String(name).replace(/[.*+?|()[]{}\]/g, '\$&');
 
        if (name in c) { return c[name]; }
 
        var match = RegExp('[?&]' + name + '=([^&]*)')
                        .exec(window.location.search);
 
        return match && (
            c[name] = decodeURIComponent(match[1].replace(/+/g, ' '))
        );
 
    };
 
}());
]]>