.split () not working properly in IE8

I use the following to extract variables from the URL contained in a variable. It works great in modern browsers, but in IE8 it fails in the first variable, but succeeds in 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:

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

+5
source share
5 answers

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

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

, split regex http://blog.stevenlevithan.com/archives/cross-browser-split. split regex, xregexp, , .

+6

p.match(regex):

http://jsfiddle.net/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);
+2

, , GET URL-.

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;
};

.

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

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

match exec:

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

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


alert(siz)

/*  returned value: (Array)
height=400, width=300
*/
0

All Articles