0

I'm just learning to work with java script and I have run into a problem with cookies in IE. the following works in all browsers except IE (ver 8 and 11 tested). Debugging has led me to the fact that the trim() of spaces is where it is stopping in IE. But I've reached the limit of my knowledge as to how to change the code to make it work. Any suggestions would be greatly appreciated. Thanks Joe

function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
    var c = ca[i].trim();
    if (c.indexOf(name) == 0)
        return c.substring(name.length, c.length);
}
return "";

}

Joe Motta
  • 1
  • 1
  • possible duplicate of [.trim() in JavaScript not working in IE](http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie) – JJJ Nov 22 '14 at 10:17

1 Answers1

0

Problem solved - here is the code I worked up with that works all browsers.

function getCookie(cname) {
    "use strict";
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i=0; i<ca.length; i++) {
        var str = ca[i];
        var c = str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
        if (c.indexOf(name) == 0)
            return c.substring(name.length, c.length);
    }
    return "";
}
Mathlight
  • 6,436
  • 17
  • 62
  • 107
Joe Motta
  • 1
  • 1