[英]This code doesn't work in IE
Hi so I have this jQuery/JS script. 嗨,我有这个jQuery / JS脚本。 That basically takes an encoded URL string and parses it and saves variable values into variables.
这基本上需要一个编码的URL字符串并将其解析并将变量值保存到变量中。
Basically what PHP's $_GET does. 基本上,PHP的$ _GET是做什么的。
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:
从此URL字符串:
/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浏览器
IE doesn't support decodeURIComponent
or so I've read. IE浏览器不支持
decodeURIComponent
,所以我已经读过。 In any case, I tried using other functions like decodeURI
and escape
all producing unwanted results. 无论如何,我都尝试使用其他函数(例如,
decodeURI
并escape
所有产生不必要的结果的函数。
My google searches have yielded nothing but, semi-interesting articles (totally off-topic but thought I'd just share that). 我的Google搜索只产生了半有趣的文章 (完全没有主题,但我认为我会分享)。
No solutions. 没有解决方案。 Can anyone shed some light?
谁能阐明一些想法? How do I make this work on IE?
如何在IE上进行这项工作?
You have read wrong, decodeURIComponent
works just fine in IE , even in IE6. 您读错了,即使在IE6中,
decodeURIComponent
在IE中也能正常工作。
However, trim
doesn't work in IE browsers prior to IE 9 and that's what causing your script to crash. 但是,
trim
在IE 9之前的 IE浏览器中不起作用,这就是导致脚本崩溃的原因。
For working alternative, see the accepted answer here: .trim() in JavaScript not working in IE 有关工作的替代方法,请参见此处的可接受答案: JavaScript中的.trim()在IE中不起作用
Or use the jQuery trim()
method as you're already using jQuery anyway. 还是使用jQuery
trim()
方法,因为您已经在使用jQuery。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.