indexOf() method throwing error in Chrome ONLY

this line…

[FONT=Courier New]if (loc.indexOf('localhost') != -1) {[/FONT]

is throwing error in Chrome ONLY (not even in Safari…)

error:

Uncaught TypeError: Cannot call method ‘indexOf’ of null

why is this, please… (‘loc’ var is fine, obviously, since no error occurs in any other browsers…)

thank you…

Well… according to Chrome, it’s not fine. In Chrome, the variable loc is null. The one line your posted doesn’t give us any clue why that might be. You’ll have to show us where loc gets its value.

here’s where this line occurs:

http://mayacove.com/misc/js_ajax_mail-mob.js

error is thrown by this line:

if (loc.indexOf('localhost') != -1) {

the var is declared in another .js file to which this one has access, namely…
http://mayacove.com/misc/js_val-mob.js

they’re both included in standard conventional form in header of html,
js_val-mob.js is called first…

and why would this var not work only in Chrome??? this doesn’t make sense…

thank you…

Have you checked using the debugger to confirm that loc is being assigned a value in each browser?

I’m testing with console.log() right now… it prints fine in Chrome, prints what var evaluates to (current url)

but that line ( if (loc.indexOf(‘localhost’) != -1) { ) just throws an error in Chrome… man, this is bizarre (doesn’t even happen in Safari…)

thank you…

after running numerous tests, have determined that Chrome simply won’t play ball with that var inside this fn:

[FONT=Courier New]subBtn.click(function(e) {  .....  }[/FONT]

even if I decl a new var inside that function Chrome throws the exact same error…

I think it has a prob with indexOf() method, but only inside the function…
[FONT=Courier New]


if (testVar.indexOf('localhost') != -1) {
			console.log('bbb')
		}

[/FONT]

prints fine outside the function… inside the function it throws that error…

this is weird…

inside the function:

if I do

[FONT=Courier New]console.log(testVar)[/FONT]

prints fine…

but if I do

[FONT=Courier New]
if (testVar.indexOf('myString') != -1) {
      console.log('bbb')
}[/FONT]

get error on indexOf()

If you post a link to the page that shows this error, we may be able to tell you why.

ok, here’s recreation…

http://mayacove.com/resume-rd/test_chrome.jsp

error occurs when you click on “send”

js is here… http://mayacove.com/resume-rd/js/js_ajax_mail-mob.js

thank you…

I got an error, but not the one you describe. Any chance that you’re using a beta build of Chrome?

oh brother… this gets more and more bizarre…

this is my version of Chrome: 26.0.1410.65 (and as far as I know, it’s not a beta version…:wink:

I don’t know why you get that error… I don’t even get to send mail part, b/c my error occurs as soon as you hit “send”…

ok, I included that file now… still get exact same error… screenshot:
http://mayacove.com/resume-rd/error.gif

again, this error occurs as soon as I hit “send” button…

man, this is truly weird…

thank you…

I don’t get the error, but I can see that loc is a global variable containing window.location href.

In case it’s conflicting with another native global, you could try renaming it. Better still just pass window.location href in its place.

ok… you’re right: I had to do

window.location.href

instead of var name…

this is very weird… but well, finally got it to work error-free…

thank you very much…