SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    indexOf() method throwing error in Chrome ONLY

    this line..

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

    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...

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by maya90 View Post
    'loc' var is fine, obviously, since no error occurs in any other browsers..
    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.
    "First make it work. Then make it better."

  3. #3
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    here's where this line occurs:

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


    error is thrown by this line:

    Code:
    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....

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,823
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Have you checked using the debugger to confirm that loc is being assigned a value in each browser?
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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...

  6. #6
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    after running numerous tests, have determined that Chrome simply won't play ball with that var inside this fn:

    subBtn.click(function(e) { ..... }

    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..
    Code:
    if (testVar.indexOf('localhost') != -1) {
    			console.log('bbb')
    		}


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


    this is weird...

  7. #7
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    inside the function:

    if I do


    Code:
    console.log(testVar)

    prints fine...

    but if I do

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


    get error on indexOf()

  8. #8
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    If you post a link to the page that shows this error, we may be able to tell you why.
    "First make it work. Then make it better."

  9. #9
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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...

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,276
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    I got an error, but not the one you describe. Any chance that you're using a beta build of Chrome?

    "First make it work. Then make it better."

  11. #11
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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....;-)

    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....

  12. #12
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by maya90 View Post
    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 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.
    Tab-indentation is a crime against humanity.

  13. #13
    SitePoint Guru
    Join Date
    Jun 2009
    Posts
    820
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •