最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - .split() not working as expected in IE8 - Stack Overflow

programmeradmin3浏览0评论

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 = ';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:

Here is the working solution... /

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.?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:

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

Share Improve this question edited Feb 29, 2012 at 1:00 Adam Youngers asked Feb 28, 2012 at 20:59 Adam YoungersAdam Youngers 6,7498 gold badges39 silver badges53 bronze badges 1
  • Split has various bugs in its implementation on various web browsers. Some are edge cases, others not. I suggest you take a look at the following to see if you can replicate the bug and then use that information to devise a workaround. blog.stevenlevithan./archives/cross-browser-split – michaelward82 Commented Feb 28, 2012 at 21:05
Add a ment  | 

5 Answers 5

Reset to default 6

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

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

As browsers have some bugs using split with a regex http://blog.stevenlevithan./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.

Use p.match(regex) instead:

http://jsfiddle/B42tM/3/

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

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

There have been some valid responses, but you may be interested in a function I use to retrieve GET parameters from URLs.

var get = function (name, url) { // Retrieves a specified HTTP GET parameter. Returns null if not found.
    url = (typeof (url) === "undefined" ? window.location.href : url);

    var regex = new RegExp("[?&]" + name + "=([^&#]*)");
    var results = regex.exec(url);
    var output = (results ? results[1] : null);

    return output;
};

You could use it like this.

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

var h = get("height",url);
var w = get("width",url);

There is a normalisation script which should fix the inconcistencies you are seeing. http://blog.stevenlevithan./archives/cross-browser-split

You can find both dimensions with a match or exec expression:

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

var siz=p.match(/((height|width)=)(\d+)/g);


alert(siz)

/*  returned value: (Array)
height=400, width=300
*/
发布评论

评论列表(0)

  1. 暂无评论