"Should," not "must." :) It's just bad form.Quote:
It means that the prototype $ function isn't valid too?? And is it a should or a must??
"Should," not "must." :) It's just bad form.Quote:
It means that the prototype $ function isn't valid too?? And is it a should or a must??
Thanks for clearing the point Twey.
You're not wrong. There would be overhead, of course, but then a counter argument is that the function would be called so infrequently that the relatively small amount of overhead that would occur would be insignificant. There's no harm in caching the results (though calling code could do that, too), but I don't think it's a big deal.Quote:
Originally Posted by Twey
An alternative is the URL property of the document object, which should also be well supported.Quote:
Also, that script fails to take into consideration the non-existance of window.location.search,
That's not quite the point though, is it? The HTML Specification recommends in appendix B.2.2 that both HTTP and CGI (or other server-side language) implementors accept semicolons to allow markup authors to forego the hassle of encoding ampersands as entity references in URLs in attributes.Quote:
Originally Posted by DimX
Why an array? You aren't using any array features, but rather a feature of objects in general; an Object object is all that's necessary.Quote:
var $_GET = new Array();
Machine-generated identifiers.Quote:
Originally Posted by Twey
You use a for..in loop to iterate over an array. That's not particularly sensible, particularly as for..in iterative statements are prone to enumerating user-defined properties inherited through the prototype chain, and even host properties that aren't assigned the DontEnum attribute. You should use a for statement, instead, particularly due to the fact that the only properties you are interested in are array indices.Quote:
and I still don't see what's wrong with my original:
The Prototype library is poor full-stop. The fact that it flouts an established convention is just one reason why.Quote:
Originally Posted by shachi
It is defined as such in the language specification, and there is no reason to disregard that reservation: there are other, less obtuse means of naming or prefixing identifiers.Quote:
And is it a should or a must??
Mike
Yes, but the point was that one might as well save that overhead anyway, particularly when it takes more code to do it the slightly-less-efficient way.Quote:
Originally Posted by mwinter
I do? Whoops...Quote:
You use a for..in loop to iterate over an array.
Code:<script type="text/javascript">
var _GET = window.location.href.substr(window.location.href.indexOf('?') + 1).split(/&|;/);
for(var i = 0; i < _GET.length; ++i)
if(_GET[i].indexOf('=') === -1)
_GET[unescape(_GET[i])] = '';
else _GET[unescape(_GET[i].substr(0, _GET[i].indexOf('=')))] = unescape(_GET[i].substr(_GET[i].indexOf('=') + 1));
</script>
Umm:Quote:
Originally Posted by Twey
More code, you say? :p That (only lightly tested, by the way) also copes with fragment identifiers, which seem to have been forgotten in other solutions.Code:function getQueryValue(name) {
var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);
return match ? unescape(match[1]) : null;
}
Mike
Bah. Foiled. :p Well, that pretty much obsoletes the rest.
I'm not as up on regex as I should be, so I have to ask: Could one use a .match() operation or two to fill an object with the values as I did with mine?
You could, though it wouldn't be a simple one-step process.Quote:
Originally Posted by Twey
When a string or non-global regular expression object is passed to the String.prototype.match method, it's exactly the same as calling the RegExp.prototype.exec method:
If pattern was a string, it's passed to the RegExp constructor function, first.Code:pattern.exec(string)
On the other hand, if the argument is a regular expression object with the global flag set, the return value is array of matches. Unfortunately, each element is the matched substring, rather than the actual result from the exec method. For example, if you were to use the regular expression:
you'd get results like "?foo=bar" and "&flag="; the capturing parentheses are ignored and might as well not exist. As such, you'd still need to strip the first character and split on the equals symbol.Code:/[?&;]([^=]+)=([^&;#]*)/
By the way, the latter result is how flags have to represented in the query string to be matched by the getQueryValue function. Furthermore, to differentiate a flag (which would be returned as an empty string) and a non-existent value, compare the return value with null.
Mike
Can you please explain why in this function:
you used this line:Code:function getQueryValue(name) {
var match = (new RegExp('[?&;]' + name + '=([^&;#]*)')).exec(document.URL);
return match ? unescape(match[1]) : null;
}
whileCode:return match ? unescape(match[1]) : null;
works fine?Code:return match[1];
What exactly does the syntax mean?
It means:
Because if no matches are found it's better to return null.Code:if (match)
return unescape(match[1]);
else
return null;
ok, so why the unescape() if it works without it?