I know that Internet Explorer <9 has issues with Javascript arrays. I've tried using stopgaps but I can't for the life of me get this figured out.
Basically, I have this code (pared way down):
YUI.add('query-params', function (Y) {
Y.QueryParams = {
/**
Parses our query string and returns an object containing the k/v pairs passed
in rules
@method parse
@param {String} query string to parse
@param {RegExp} regex rules to compare our query string against
@return {Object} returns an object containing the k/v pairs that match
*/
parse: function (qs, rules, params) {
qs = qs.replace('?', ''); // remove ? from query string
qs = qs.replace(/\+/g, ' ');
var matches = rules.exec(qs);
var defined_matches = Y.Array.filter(matches, function(m) {
return ((typeof m != 'undefined') || ( m == true ));
});
// rest of script omitted
And it is initialized as such:
<script>
YUI({
modules: {
'query-params': {
fullpath: './parse_search_terms.js'
}
},
onFailure: function (error) {
console.log(JSON.stringify(error));
}
}).use('node', 'console', 'query-params', function (Y) {
var params = Y.QueryParams.parse(window.location.search, /(\w+)\s(\w+)\s(\w+)|(\w+)\s(\w+)/ig, ["firstName", "lastName", "state"]);
var url = "url_to_send_to?firstName=" + params.firstName + "&lastName=" + params.lastName + "&state=" + params.state;
var iframe = Y.Node.create('<iframe></iframe>').setAttrs({
src: url,
frameBorder: 'no',
height: 300,
width: 800,
scrolling: 'no'
});
Y.one('#iframe').appendChild(iframe);
});
What I get in IE, Firefox and Chrome when I pass a query string like "q=john+smith+ca" and JSON.stringify the matches variable is this:
john smith ca,john,smith,ca,,
Firefox and Chrome do the right thing when I call Y.Array.filter on the array:
defined ["john","smith","ca"]
Internet Explorer 8 however:
defined ["john","smith","ca","",""]
So for some reason IE isn't filtering my array properly. I've tried all the aforementioned array shims and stopgaps but nothing seems to work. I would think that this would be pretty cross browser efficient since I'm using YUI3, but I'm stumped.
Anyone have any ideas? Help much appreciated.