1

I have the following lines of Javascript:

function SetTabIndex() {
    var test = document.getElementsByTagName("input");

    for (var i = 0; i < test.length; i++) {

        var cssText = test[i].getAttribute('style'),
            tabindex = cssText.match(/tabindex\s*:\s*(\d+)/);

        if (tabindex) {
            test[i].setAttribute('tabindex', tabindex[1]);
        }
    }
}

When I run this locally, the script runs and does exactly what I want it to. But, when I put this on my target system (which only has IE8 and can't be updated) the code doesn't run.

I've checked my log and I get the following error:

Object doesn't support this property or method

When I click the link next to it, it takes me to the following line of code:

tabindex = cssText.match(/tabindex\s*:\s*(\d+)/);

I've tried taking out some of my regex code, but it still throws up an error. When I take that line away and throw in some console outputs, everything works fine.

Is there a particular reason as to why this doesn't work on IE8 even though I tested it locally on my more updated version of IE with it set to emulate IE8 just fine?

Tushar
  • 85,780
  • 21
  • 159
  • 179
N0xus
  • 2,674
  • 12
  • 65
  • 126
  • Is `cssText` the same type and value in all of the browers? Maybe IE8 returns something other than a string, which might not have the `match()` function available. – Cᴏʀʏ May 12 '15 at 12:50
  • The above code is literally copied and pasted from my local machine to the other. How can I check to see what match returns? – N0xus May 12 '15 at 12:51
  • `console.log` it out to inspect it, or add additional debug code. You can check the type with `typeof()`. – Cᴏʀʏ May 12 '15 at 12:55

1 Answers1

2

In Internet explorer 8 JScript regular expression bug the following answer was given:

The issue here was that "\s" in javascript does not include a non breaking space in IE but includes a non breaking space in FF.

Community
  • 1
  • 1
  • So how can I fix the issue? I just removed the \s but I still get the same error – N0xus May 12 '15 at 13:05
  • @N0xus: Specify the character class directly, instead of using `\s`. – nhahtdh May 12 '15 at 13:19
  • @nhahtdh how do I do that? I've never really used this stuff before – N0xus May 12 '15 at 13:21
  • @N0xus: There are 2 ways: `[\s\u00a0]` (include no break space along with `\s`), **or** manually specify `\s` (which is quite long, if you may encounter EN SPACE, EM SPACE, or IDEOGRAPHIC SPACE). – nhahtdh May 13 '15 at 02:50