0

Hi so I have this jQuery/JS script. That basically takes an encoded URL string and parses it and saves variable values into variables.

Basically what PHP's $_GET does.

function getUrlVars() 
{
 var map = {}; 
 var parts = window.location.search.replace(/[?&]+([^=&]+)(=[^&]*)?/gi, 

   function(m,key,value) 
     { map[key] = (value === undefined) ? true : value.substring(1); }); 

 return map; 
}

Basically this script does what I want. From this URL string:

/autopop.html?Email=test%40test.com&LastName=Test+last&&FirstName=+Test+First

I get the values:

Email = test%40test.com

LastName = Test+last

FirstName = +Test+First

What I want to do is Auto-populate a form on this same page with this information. (I know what you're thinking a server-side script would be a better solution but the boss says we don't have access to that, trust me, I've tried)

Long story short, here's the rest of my code:

var keys = getUrlVars();

$(document).ready(function(){
    var fname = keys['FirstName'].replace(/\+/g , " ").trim();
    var lname = keys['LastName'].replace(/\+/g , " ").trim();
    var email = decodeURIComponent(keys['Contact0Email'].replace(/\+/g , " ")).trim();
    $("#Email").val(email);
    $("#FirstName").val(fname);
    $("#LastName").val(lname);
});

This code gets the job done. All except for one browser. IE.

IE doesn't support decodeURIComponent or so I've read. In any case, I tried using other functions like decodeURI and escape all producing unwanted results.

My google searches have yielded nothing but, semi-interesting articles (totally off-topic but thought I'd just share that).

No solutions. Can anyone shed some light? How do I make this work on IE?

rgin
  • 2,291
  • 4
  • 24
  • 32
  • What do the unwanted results look like? – Pekka Apr 12 '11 at 07:04
  • Clean Text? It's not the resulting text that I'm having problems with it's browser compatibility. This works in all other browsers except IE8 and below.IE9 apparently supports it. – rgin Apr 12 '11 at 07:20
  • then IE8 should show you an error. What does that error say? – Pekka Apr 12 '11 at 07:30
  • @pekka I don't remember anymore. I just recently upgraded to IE9. But it said something about a function not being supported. I thought it was decodeURIComponent at first but as @Shadow pointed out below, it's the .trim() function that's causing the crash. I'll need some time to test it but it seems to be the right answer. – rgin Apr 12 '11 at 07:48
  • fair enough. In future questions, always remember to add the exact error message - it is usually impossible or very hard to tell what's wrong only from seeing the code. Thanks! – Pekka Apr 12 '11 at 08:57

1 Answers1

8

You have read wrong, decodeURIComponent works just fine in IE, even in IE6.

However, trim doesn't work in IE browsers prior to IE 9 and that's what causing your script to crash.

For working alternative, see the accepted answer here: .trim() in JavaScript not working in IE
Or use the jQuery trim() method as you're already using jQuery anyway.

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208