10

I get the following error and iPad but not in desktop browsers:

JavaScript: Error
undefined
TypeError: 'undefined' is not a function

This is a larger js application, and this error message is totally unhelpful. Is there any way I can get the line number of the error or anymore information?

Update: This just got funky.

line : 0    
page : undefined
desc : TypeError: 'undefined' is not a function
chr  : undefined

Did the user agent spoofing in FF and safari. No error.

Fresheyeball
  • 29,567
  • 20
  • 102
  • 164
  • 2
    I had the same issue, and while the window.onerror suggestion was a start, I got the same unhelpful results. I ended up having to litter my scripts with console.logs to narrow in. With iOS it feels like we've backtracked to ie6 days with error debugging... My specific issue was that I was assuming function(){}.bind() was supported in my app. It wasn't, but my dev browsers (Safari/Chrome) do support it natively, while iOS Safari apparently does not. – heff May 03 '12 at 17:11
  • Believe it or not, `.bind` was my issue too. – Fresheyeball May 03 '12 at 20:48

2 Answers2

13

You could try registering a custom error handler to window.onerror

window.onerror = function (desc,page,line,chr)
{ alert('Line:'+line); }

desc = Error message
page = File/Page where the error occured
line = Well...
chr = Character position of the error in the line

bardiir
  • 14,556
  • 9
  • 41
  • 66
  • try the `page` variable :) - i've edited the answer to include the content information for all four variables :) – bardiir Apr 17 '12 at 19:44
  • for page I get 'undefined', weird – Fresheyeball Apr 17 '12 at 19:49
  • That can happen if the code is loaded via ajax or eval'd into place. You only got a closure or some objects floating in the javascript machine without any existing code in any file. The Javascript code just existed inside a variable at runtime. - This can also happen if the code is compressed by dean.edwards /packer/ for example. Try to use unpacked/unminified versions of all files if available. – bardiir Apr 17 '12 at 19:51
  • Can you reproduce the error inside, let's say safari when you switch the UserAgent for the browser to iPad? Maybe it's a glitch in some browser specific code. Once you got it to throw errors in safari you could insert breakpoints and see the "undefined" code sequences in the code view. – bardiir Apr 17 '12 at 19:56
  • Yeah once you're in twilight you also might want to try FirebugLITE - http://getfirebug.com/firebuglite - That's runnable on the iPad too :) – bardiir Apr 17 '12 at 20:00
  • I tried that actually. Error occurs before firebug will open. – Fresheyeball Apr 17 '12 at 20:01
  • Use firebug in a html document framing the errornous page on the same host/domain. You should be able to see everything happening in the iframe too as there is no crossdomain block. - Using that you can load the errornous page with a x second delay. – bardiir Apr 17 '12 at 20:04
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10198/discussion-between-fresheyeball-and-bardiir) – Fresheyeball Apr 17 '12 at 20:18
2

If you bind an error handler to window.onerror, it should give you the line number, e.g.

window.onerror = function(msg,url,line) {
   alert('The error is on line '+line);
}

This question: Debug JavaScript errors on iPad seems to indicate you can enable debugging too.

If the script is loaded dynamically, though, it can be hard to get such info in any environment.

Community
  • 1
  • 1
Jamie Treworgy
  • 23,934
  • 8
  • 76
  • 119