.split()在IE8中没有按预期工作 [英] .split() not working as expected in IE8

查看:120
本文介绍了.split()在IE8中没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下内容从变量中包含的URL中提取变量。它在现代浏览器中工作正常但在IE8中它在第一个变量上失败但在第二个变量上成功。

I'm using the following to extract variables from a URL contained in a variable. It works fine in modern browsers but in IE8 it fails on the first variable but succeeds on the second.

var p = 'http://sagensundesign.com?height=400&width=300';

/* Get Height */
var h = p.split(/height=([0-9]+)/);
h = h[1];
if (!h) {h = 500};
alert(h);

/* Get Width */
var w = p.split(/width=([0-9]+)/);
w = w[1];
if (!w) {w = 800};
alert(w);

UDPATE:

以下是工作解决方案...... http://jsfiddle.net/cssguru/B42tM/

Here is the working solution... http://jsfiddle.net/cssguru/B42tM/

推荐答案

你需要在这里使用拆分吗?你能不能只使用匹配

Do you need to use split here? Can you not just use match:

var h = p.match(/height=([0-9]+)/)[1];

因为浏览器有一些错误使用split与正则表达式 http://blog.stevenlevithan.com/archives/cross-browser-split 。如果你确实需要使用正则表达式跨浏览器 split ,你可以查看 xregexp 这是一个修复浏览器中正则表达式的库。

As browsers have some bugs using split with a regex http://blog.stevenlevithan.com/archives/cross-browser-split. If you do need to use split with a regex cross browser you could look at xregexp which is a library that fixes regexs across browsers.

这篇关于.split()在IE8中没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆