I do something very similar in my JS. > Why not incorporate json stringifying instead?
It's very useful to mirror CGI format - e.g. how PHP and Rails use it. E.g. ?x=hello rather than JSON : x='hello' On 2 Mar, 17:40, Daniel Friesen <nadir.seen.f...@gmail.com> wrote: > Why use a custom object notation incompatible with anything else when > JSON exists? > Why not incorporate json stringifying instead? > > ~Daniel Friesen (Dantman, Nadir-Seen-Fire) [http://nadir-seen-fire.com] > -Nadir-Point & Wiki-Tools (http://nadir-point.com) (http://wiki-tools.com) > -MonkeyScript (http://monkeyscript.org) > -Animepedia (http://anime.wikia.com) > -Narutopedia (http://naruto.wikia.com) > -Soul Eater Wiki (http://souleater.wikia.com) > > John Resig wrote: > > Kevin - > > > Something like this we'd like to test out in the realm of plugins > > first - just release your work as a plugin (be sure to toss it up on > > plugins.jquery.com and link to some demos) and if people really start > > to use it we'll definitely consider it for core. That's generally how > > we evaluate most code that goes in to core (look at how plugins > > handled the problems first, then refine them). > > > --John > > > On Sun, Mar 1, 2009 at 5:19 PM, Kevin Dalman <kevin.dal...@gmail.com> wrote: > > >> I am working on a project and need URL param-parsing, as I usually do. > >> But this time I decided to try something new - based on ColdFusion > >> syntax that I have used for ages. > > >> ColdFusion creates a system-level hash structure named "URL" that > >> contains all the URL params. This is very convenient, so I created a > >> jQuery extension to do the same thing. It creates and populates a > >> "$.url" hash on-load. This is a static var similar to $.browser. > > >> The $.url object does the *opposite* of the $.serialize method: > >> $.serialize converts complex data TO an URL format, while $.url > >> creates a data object FROM the URL params. > > >> My initial version was very small - only a few bytes of code is needed > >> to parse simple parameters. This would address the basic needs of most > >> users. So I think this *at a minimum* would be a valuable addition to > >> the jQuery core. The code is very small, creates no conflicts, and > >> takes barely 1ms. > > >> After I created the basic method, I added more elaborate parsing to > >> store numbers and booleans in their proper format, and to allow > >> 'complex data' to be passed - ie, arrays and hashes: > > >> page.html?actors=[Eastwood,Bronson,Heston] > >> page.html?actor={first: Clint, last: Eastwood} > > >> This includes automatic array creation when a param key is repeated: > > >> page.html?actor=Eastwood&actor=Bronson&actor=Heston > > >> ...becomes: actor=[ Eastwood, Bronson, Heston ] > > >> This also allows arrays-of-arrays and arrays-of-hashes: > > >> page.html?actor={first: Clint, last: Eastwood}&actor={first: Charles, > >> last: Bronson} > > >> ...becomes: > > >> actor = [ > >> 0: { > >> first: 'Clint' > >> , last: 'Eastwood' > >> } > >> 1: { > >> first: 'Charles' > >> , last: 'Bronson' > >> } > >> ] > > >> You can see and test a demo page here... > > >> http://layout.jquery-dev.net/url_parsing.html > > >> There are a number of test URLs (hyperlinks) provided to demonstrate > >> the different types of parsing, but you can append any params you want > >> to the URL to see how they are parsed. > > >> To illustrate the size of the 'long version', here is the partially > >> minified code: > > >> // MINIFIED CODE (860 bytes) > >> function setURL(){ > >> $.url={};$.urlParams=[]; > >> var s=self.location.search.substr(1),p,d,k,v,i; > >> if(!s)return; > >> p=s.split("&"); > >> for(i=0;i<p.length;i++){ > >> d=p[i].split("=");k=$.trim(d[0]); > >> if(k){ > >> v=d[1]==undefined?true:parse(d[1]); > >> if(!$.url[k]){$.url[k]=v;$.urlParams.push(k);} > >> else{if(!$.isArray($.url[k])||($.isArray(v)&&typeof $.url[k][0]! > >> ='object'))$.url[k]=[$.url[k]];$.url[k].push(v);} > >> } > >> } > >> function parse(x){ > >> x=$.trim(x); > >> if(!x)return ""; > >> var c=x.length-1,f=x.charAt(0),l=x.charAt > >> (c),A=f=="["&&l=="]",H=f=="{"&&l=="}",d,h,k,o,i; > >> if(A||H){ > >> o=A?[]:{};d=x.substr(1,c-1).split(",") > >> for(i=0;i<d.length;i++){ > >> if(A)o[i]=parse(d[i]); > >> else if(d[i]){h=d[i].split(":");k=$.trim(h[0]);if(k)o[k]=parse(h[1]);} > >> } > >> return o; > >> } > >> else if(!isNaN(x))return Number(x); > >> else if(x==="true")return true; > >> else if(x==="false")return false; > >> else return x; > >> } > >> } > > >> The demo page contains a more readable, commented version of the code > >> above. If there is any interst in this code, feel free to help > >> yourself. I did not keep a copy of the short-version, but it would not > >> take long to recreate - this is not complex code. > > >> SO, do John and the gang feel this addition would be worthwhile for > >> jQuery? I'm suggesting this partly out of self-interest - I copy the > >> same URL-parsing functions to every project I work on. I'd prefer that > >> this basic functionality was part of jQuery, and I feel a $.url object > >> is the most intuitive and flexible way to do it. > > >> At a minimum, this will become part of my standard jQuery extensions > >> library. I prefer working with an 'URL object' rather than using a > >> 'parsing method': > > >> // using an URL Object > >> if ($.url.section) doSomething( $.url.section ); > > >> // using a Parsing Method > >> var section = parseURL('section'); > >> if (section) doSomething( section ); > > >> The ability to use complex objects offers more options for passing JS > >> data between pages: > > >> $.each($.url.actor, function (idx, Actor) { > >> $('#List').append('<li>'+ Actor.first +' '+ Actor.last +'</li>'); > >> }); > > >> The example above is a little silly, but you see how it could be > >> useful for passing 'state' or other data. > > >> Feedback? > > >> /Kevin --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "jQuery Development" group. To post to this group, send email to jquery-dev@googlegroups.com To unsubscribe from this group, send email to jquery-dev+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/jquery-dev?hl=en -~----------~----~----~----~------~----~------~--~---