3

So this one honestly has me scratching my head. I have a single line of code in my Javascript that does not work when deployed to our test server, but works just fine when run locally from Visual Studio. There's also no problems when running the function in Chrome, regardless of location. It's just a simple line to get the last character of a string, so it's really confusing me why it just stopped working. I'm running IE8 both locally and on the test server (Same machine is browsing, just moved the host).

WhichCredit = WhichCredit[WhichCredit.length - 1];
6
  • 3
    it would help if you specify the version of IE you're testing with. And is it the same version when you test locally vs when you test online? Commented May 29, 2012 at 21:12
  • Sorry, yes they're both being viewed by IE8. Actually on the same machine, the only difference being we moved the host to our test server. Commented May 29, 2012 at 21:16
  • 1
    okay, and you're certain it's displaying in the same mode in both instances? is it possible that it's displaying in IE7 compatibility mode in one case? this would make it run an older version of the JS interpreter (among other things), which could cause the kind of effect you're seeing. To check this, open Dev Tools (F12). Commented May 29, 2012 at 21:18
  • I'll mark you as an answer Spudley if you put an answer down. Turns out my browser defaults to IE8 Compatibility mode when run straight from the desktop but straight IE8 when run from Visual Studio. Thanks for the help. I feel like an idiot now. >_> Commented May 29, 2012 at 21:22
  • ps - don't feel like an idiot -- this catches a lot of people out. IE has a nasty habit of defaulting to compatibility mode under some circumstances but not others, and not telling you. Commented May 29, 2012 at 21:29

2 Answers 2

5

Accessing characters of a string using bracket notation was introduced in ECMAScript 5. It's possible that the javascript interpreter is old and doesn't support grabbing characters from a string using bracket notation. You'd be far better off using a built-in function, such as WhichCredit.substr(-1) or WhichCredit.charAt(WhichCredit.length - 1) to do this.

Sign up to request clarification or add additional context in comments.

3 Comments

But I thought that the Javascript interpreter was solely in the browser? It's the same browser, the only difference being whether the page is local or not.
I'm glad you found the cause of the problem, but the fact remains that in some older browsers this will still pose a problem, and using the older syntax will be beneficial, and in most cases easier to read.
I realize this and will indeed be changing them eventually, but as it is, it's not feasible to switch right this instant.
3

It may be in the same browser, but are you certain it's displaying in the same mode in both instances?

Is it possible that it's displaying in IE7 compatibility mode in one case? this would make it run an older version of the JS interpreter (among other things), which could cause the kind of effect you're seeing. To check this, open Dev Tools (F12).

Hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.