iPad JavaScript Error not helpful - javascript

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.

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

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.

Related

Obtain backtrace from window.addEventListener('error') in Chrome?

I am trying to log errors on devices, that don't have a builtin javascript console (e.g. mobile). In the process I was trying to capture errors using window.addEventListener('error', (event)=>{...}).
In Firefox, the entry event.error gives access to full information about the error. In Chrome however, event.error is always null. Similarly, the equivalent error-object that is passed as one of the arguments to window.onerror is present in Firefox, but missing in Chrome.
Is this expected behavior? Is there some reliable cross-browser method of accessing the backtrace information?
Example
Execute in the console, e.g. on this site.
window.addEventListener('error', (err) => {
for(const key in err) {
console.log(key,'=',err[key]);
}
});
// Chrome doesn't capture errors produced directly in the console,
// so we use a timeout.
window.setTimeout(()=>{ noSuchVariable; }, 100)
I obtain:
VM5685:3 isTrusted = true
VM5685:3 message = Script error.
VM5685:3 filename =
VM5685:3 lineno = 0
VM5685:3 colno = 0
VM5685:3 error = null
...
I obtain the same result in private mode, i.e. it is not related to extensions.
Firefox give the much more helpful result:
sTrusted = true debugger eval code:3:17
message = ReferenceError: noSuchVariable is not defined debugger eval code:3:17
filename = debugger eval code debugger eval code:3:17
lineno = 8 debugger eval code:3:17
colno = 25 debugger eval code:3:17
error = ReferenceError: "noSuchVariable is not defined"
<anonymous> debugger eval code:8
As pointed out by CertainPerformance, the issue was caused by using the Chrome developer console, which interferes with error handling.
The error data is correctly given however, if the error is thrown inside a timer, or inside a script executed by typing a javascript: URL.

RegExp uncaught TypeError: Cannot read property '1' of null

I found this code;
navigator.appVersion.match(/MSIE ([\d.]+)/)[1];
For detecting browser version. This code works in IE 8 below but not on google chrome. The error in google chrome says;
Uncaught TypeError: Cannot read property '1' of null
and it points to the line where this code belongs;
navigator.appVersion.match(/MSIE ([\d.]+)/)[1];
Any idea how to fix this issue?
just to make things clear what i'm trying to accomplish here is to detect the browser version:
var version = navigator.appVersion.match(/MSIE ([\d.]+)/)[1];
if(version <= 8.0)
{
execute code;
}
Everything works fine in IE but I got an error in google chrome which is:
Uncaught TypeError: Cannot read property '1' of null
and it points to the line where this code belongs;
navigator.appVersion.match(/MSIE ([\d.]+)/)[1];
match can return null if regexp don't find anything. So you must first get matches and check have you any match or not. Try this:
var ieMatches = navigator.appVersion.match(/MSIE ([\d.]+)/);
var isIE = !!ieMatches[1];

exception.lineNumber returns "undefined" in google chrome and internet explorer

I need to get fileName, Message, LineNumber etc from a javascript exception. I tried the following code.
try {
alertt("dddd");
} catch (e) {
console.log("ExceptionType: "+ e.name);
console.log("Message: "+ e.message);
console.log("Line No: "+ e.lineNumber);
}
I got the following result in Mozilla Firefox
ExceptionType: ReferenceError
Message: alertt is not defined
Line No: 4
But the same code gave the following result in Google Chrome, Internet Explorer
ExceptionType: ReferenceError
Message: alertt is not defined
Line No: undefined
It is not giving the Line Number. How to solve this issue? Is there any another method for getting Line number?
I tried e.stack It returns the stack trace as string.
It gave me the following output in Google Chrome
ReferenceError: alertt is not defined
at message (http://localhost/ems-test/js/test.js:4:4)
at HTMLDocument.<anonymous> (http://localhost/ems-test/js/test.js:14:2)
at c (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26036)
at Object.p.fireWith [as resolveWith] (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26840)
at Function.x.extend.ready (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:3305)
at HTMLDocument.q (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:717)
and firefox gave this result
message#http://localhost/ems-test/js/test.js:4
#http://localhost/ems-test/js/test.js:14
x.Callbacks/c#http://localhost/ems-test/js/jquery-1.10.2.min.js:4
x.Callbacks/p.fireWith#http://localhost/ems-test/js/jquery-1.10.2.min.js:4
.ready#http://localhost/ems-test/js/jquery-1.10.2.min.js:4
q#http://localhost/ems-test/js/jquery-1.10.2.min.js:4
Both are string type result. Not an object. So it needs to extract the line number from this huge string. But the problem is both result are not same. One shows the line number in first line and another one shows it in second line. So it will be difficult to extract line number from this huge string.
Is there any method to get the stack trace as an object?
window.onerror = function (msg, url, line) {
alert("Message : " + msg );
alert("url : " + url );
alert("Line number : " + line );
}
Hope this might help you.
check on this link: http://www.tutorialspoint.com/cgi-bin/practice.cgi?file=javascript_40
I don't have IE on my machine, so I can't speak for that; but in Chrome, you might be able to get what you need by looking at the e.stack property and parsing it out. You can see the options available to you if you do a console.dir(e) within your catch block.

How to catch all javascript warnings and errors to an output div?

i'm using try, catch, for debugging, but warnings is not create exceptions. How to get all javascript warnings and errors to output div?
UPDATED:
If browser supports Afaik logging, how to get that log to string or output div?
UPDATED:
I found the way how to do that:
i can reload console.log function to my custom function an call native console.log function.
First of all, get rid of the try catch. Don't use try catch when you are debugging.
Second, you don't want to out errors to a div, use firebug or inspector for that - console.log();
Third, if you really want to do it: you could use try catch and in the catch, use something like
$('body').append($('div').html('variable for error message goes here'));
if you are using jquery
OR
document.getElementByTagName("body").appendChild( document.createTextNode("variable for error message goes here") );
if you have plain javascript
EDIT: try looking up ie debug bar , ie webDeveloper
I understand myself why someone may want something to actually happen when an error occours in the document. The answers above just say that you would use developer tools, but I needed things to actually happen, and after some searching, here's what I found...
If you wish to catch all errors that come through, you can put the following code into your file, best at the top:
window.onerror = function(errorMsg, url, lineNumber){
// any action you want goes here
// errorMsg is the error message itself.
// url should be the file presenting the error, though i have
// found that it only presents to me the address of the site.
// lineNumber is the line number the error occoured on.
// here is an example of what you could do with it:
alert("Error in " + url + " at " + lineNumber + ":\n" + errorMsg);
}
I, myself, like to output the errors to a div that contains them all, though you can do literally anything to this information that you could do with any other string passed to a function.
Here is an example of what may happen if you throw an error with a button using the code above:
Error in your.site.here at 1:
Uncaught ReferenceError: foo is not defined
For IE javascript debugging you can follow this guide:
http://msdn.microsoft.com/en-au/library/ie/gg699336(v=vs.85).aspx
Keep in mind that the developer tools window must be open prior to loading the page for the warnings and errors to appear in the console.
For webkit, (chrome, safari) developer console - here is a guide:
https://developers.google.com/chrome-developer-tools/docs/console
...Firefox also has a console

Javascript error in IE

I have a page which is showing a javascript error in IE, stating which line the error is on, but the there are so many linked files I have no idea which line of code it is referring to.
Is there a way to find out which line / linked file it is relating to?? I'm sure I could fix the problem if I knew where it was.
Here is the error message from IE...
Message: 'null' is null or not an object
Line: 2231
Char: 5
Code: 0
URI: http://www.hpbtenancies.co.uk/location/JF/default.aspx
It happens on the line the error reports.. 2231 on default.aspx
dd.onchange = function(){ link.href = hrefOrig + dd.value;
The detailed error is
dd is null
Use Firefox which has better reporting and also use FireBug for even more flexibility in debugging javascript..
IE 8+ also has improved javascript debugging tools. In the console you will see
SCRIPT5007: Unable to set value of the property 'onchange': object is null or undefined
default.aspx, line 2231 character 5
Right-click on the page with the error, click 'View source', scroll down until you reach line 2231 - that's where your error is.

Categories

Resources