[英]JavaScript .split function does not work in all browsers
This function works in some IE browsers, but does not work in all. 此功能可在某些IE浏览器中使用,但不能全部使用。 IE 8 gives me an error:
IE 8给我一个错误:
Webpage error details 网页错误详细信息
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB7.3; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET4.0C; .NET4.0E; MS-RTC LM 8) Timestamp: Wed, 25 Apr 2012 15:18:21 UTC
用户代理:Mozilla / 4.0(兼容; MSIE 8.0; Windows NT 5.1; Trident / 4.0; GTB7.3; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET4.0C; .NET4.0E; MS-RTC LM 8)时间戳:UTC,2012年4月25日15:18:21
Message: Object doesn't support this property or method
Line: 9
Char: 17
Code: 0
URI: file:///G:/1.html
Code: 码:
GetLink();
function GetLink() {
selectedOption = "asdasdasd: asdasdas|asdadasd:asdadsasd|asdasdasd:asdasdad";
ROOM = selectedOption.split("|")[0].trim().split(":")[1].trim();
BUILDING = selectedOption.split("|")[1].trim().split(":")[1].trim();
var ret = "room_chart.jsp?room=" + ROOM + "&building=" + BUILDING;
return ret;
}
The split
method is fine, it's trim
that's causing the problem. split
方法很好,它是trim
的原因。 You can use this little polyfill from MDN : 您可以使用MDN的这个polyfill :
if(!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g,'');
};
}
String.prototype.trim
is not available in IE < 9. The snippet of code above simply adds the split
method to String.prototype
if it doesn't already exist, and behaves exactly as you would expect the native implementation to. IE <9中不提供
String.prototype.trim
。上面的代码片段只是将split
方法添加到String.prototype
如果尚不存在),并且其行为与您期望的本机实现完全相同。
If you are using jQuery, there is a $.trim
method you can use. 如果您使用的是jQuery,则可以使用
$.trim
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.