How do I determine what exception was thrown? - javascript

I work on a script that is added to hundreds of different websites in ways that I can't always predict, particularly when code is loaded in an iframe. In my code I've got a try/catch block setup to handle the exceptions. It's something like this:
var vals = {};
try {
vals.hostname = window.top.location.hostname; // will throw DOMException if loaded in iframe
< more code which might throw other exceptions >
} catch (e) {
if (e instanceof DOMException) {
vals.hostname = window.location.hostname;
} else {
<do something different>
}
}
e instanceof DOMException works awesome on Chrome, but it turns out that on Firefox and Safari, e instanceof DOMException is false on those browsers. I've done a lot of searching and for some reason can't seem to find anybody who explains how to check the exception type in a browser agnostic way.
Edit: Okay, Chrome is throwing a DOMException, but Firefox (and presumably Safari) are just throwing a generic "Error":
Error: Permission denied to access property "hostname"
I think that means I can't do anything with the specific error on other browsers.

Related

Javascript global error handling (not work on ie 9)

I would like to catch every error in javascript and log server side.
core.js (on first line)
// Global error javascript log
window.onerror = function(msg, url, line)
{
try
{
// Send error to server via AJAX Request
var x = new (this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
x.open('POST', '/jserrorlog.php', 1);
x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
x.send('Msg='+msg+'&Url='+url+'&Line='+line);
x.onreadystatechange = function(){
if(x.readyState > 3 && x.status == 200)
window.console && console.log(x.responseText);
};
}
catch(e)
{
window.console && console.log(e);
}
};
jserrorlog.php (Server Side)
<?php
// log error on server
$Msg = (isset($_POST['Msg'])) ? $_POST['Msg'] : null;
$Url = (isset($_POST['Url'])) ? $_POST['Url'] : null;
$Line = (isset($_POST['Line'])) ? $_POST['Line'] : null;
echo ( error_log('Javascript Error:'.$Msg.'; Url:'.$Url.'; Line:'.$Line) ) ? 1 : 0;
From: http://msdn.microsoft.com/en-us/library/ms976144.aspx
Handling Errors via the window.onerror DHTML Event
A common problem that bites many developers occurs when their onerror
handler is not called because they have script debugging enabled for
Internet Explorer. This will be the case by default if you have
installed the Microsoft Script Debugger or Microsoft Visual Studio
6.0® (specifically Visual InterDev 6.0™)—onerror handling is how these products launch their debugger. You can disable script debugging for a
given instance of Internet Explorer on the Advanced tab of the
Internet Options dialog box (note that checking the Disable script
debugging setting will apply only to that instance of Internet
Explorer)
Disable script debugging to invoke your own onerror handler
It should be clear from this discussion that you can catch both syntax
and run-time errors using a window.onerror handler. However, I only
told you this so that you would understand how it works, not so that
you would intentionally allow syntax errors in your production Web
pages. All syntax errors can, and should, be eliminated during the
development phase. Besides, onerror handling doesn't even work for
VBScript syntax errors (as noted below), and there is no way to catch
server-side syntax errors in any language.
Disable script debugging on IE
In Internet Explorer, choose Internet Options from the Tools menu.
In the Internet Options dialog box, click the Advanced tab.
On the Advanced tab, under Browsing, clear Disable Script Debugging.
Click OK.

Parallel.js have problems with Blob in IE

I need to execute functions in "parallel" and I use parallel.js:
var p = new Parallel(items);
var fn1 = function (item) {
doSomething(item);
};
p.map(fn1).then(function () {
otherFunction();
});
But IE shows the following error:
[Q] Unhandled rejection reasons (should be empty): (no stack) SecurityError
HTML7007: One or more blob URLs were revoked by closing the blob
for which they were created. These URLs will no longer resolve as
the data backing the URL has been freed.
How to fix this error?
I had review parallel.js page in IE and all examples work fine.
I use Durandal, Breeze and Knockout.
In Firefox shows the following error:
[Q] Unhandled rejection reasons (should be empty):
["(no stack) [Exception..... location: "<unknown>"]"]
and in Google Chrome no shows error, but parallel.js no work.
In case you're still having trouble with this, to make Parallel.js work in Internet Explorer you have to include the evalPath option, as mentioned on the website:
evalPath (optional): This is the path to the file eval.js. This is
required when running in node, and required when requiring files in
browser environments (to work around cross-domain restrictions for web
workers in IE 10).
This also applies to IE 11.
So your code will become:
var p = new Parallel(items, {evalPath: [PATH_TO_EVAL_JS]});
Where [PATH_TO_EVAL_JS] points to eval.js.

JQuery security error in Opera and Internet Explorer

I am developing an app for social network which works in IFrame. The app works just fine in Google Chrome and Microsoft Firefox browsers, but in Opera 12.15 JQuery library v1.10.1 fails to load with security error Unhandled error: Security error: attempted to read protected variable on line 1513.
The screenshot is here:
It looks like the same bug exists in Internet Explorer 10.
How to deal with it?
UPDATE:
I have made dirty hack by commenting the lines 1513-1517 in the code of jquery:
// Support: IE>8
// If iframe document is assigned to "document" variable and if iframe has been reloaded,
// IE will throw "permission denied" error when accessing "document" variable, see jQuery #13936
/*if ( parent && parent.frameElement ) {
parent.attachEvent( "onbeforeunload", function() {
setDocument();
});
}*/
The functionality of my app seems to work now, maybe it is necessary to create issue in JQuery repo...
Bug report was created - http://bugs.jquery.com/ticket/13980.
Bug is now fixed.
Add this before you include JQuery:
var isIE11 = !!(navigator.userAgent.match(/Trident/) && !navigator.userAgent.match(/MSIE/));
if (isIE11) {
if (typeof window.attachEvent == "undefined" || !window.attachEvent) {
window.attachEvent = window.addEventListener;
}
}
Hope it helps, It worked for me.

JavaScript ActiveXObject

I have a queastion about ActiveXObject in javascript. I have tryed this code in Mozila FireFox 6.0.2
var AXobj = new ActiveXObject("WScript.Shell");
AXobj.SendKeys(key);
But the error console says that ActiveXObject is undefined. After that, I have tryed this:
var AXobj = new DOMParser("WScript.Shell");
AXobj.SendKeys(key);
But then, the error console says:
Error: uncaught exception: [Exception... "Security error" code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)" location: "file:///C:/Documents%20and%20Settings/Guest/Desktop/stuff/html/GML%20to%20JS.html Line: 335"]
By the way, i don't want to use ActiveXObject only for SendKeys. I need it for more stuff (like writing in file... ) AND, the reason i use FireFox instead of IE is that FireFox supports HTML5.
ActiveX is a proprietary technology only supported by Microsoft...
It will only work in IE (thank goodness).
It also has some serious security concerns which is a big reason it was never adopted by other browser providers.
For this you can check if it is IE then do this otherwise
do that.
Like:
Function exampleFunction()
{
if ($.browser.msie) { /* IE */
//Your code
else {
//Your code
}
}
just a suggestion.

Programmatically access webpage error details in browsers

Is there some way to access webpage warning/error details using JavaScript?
For instance, errors in IE show up in the bottom left corner like so:
I would like to able to access the details of this error (in IE as well as other browsers if possible) using JavaScript.
Any suggestions?
EDIT: I'm not looking for debuggers. I want to access the content of the error details/error console. Alternately, figuring out how to create a global exception handler equivalent for JavaScript would help too
You may want to use the window.onerror event. You can consider this event as a sort of global exception handler. The value returned by onerror determines whether the browser displays a standard error message. If you return false, the browser displays the standard error message in the JavaScript console. If you return true, the browser does not display the standard error message. (Source)
<script type="text/javascript">
window.onerror=function(msg, url, line){
alert('An error has occurred' + msg);
return true;
}
</script>
<script type="text/javascript">
// Syntax error
document.write('hi there'
</script>
You can also use traditional exception handling in JavaScript to catch run-time errors.
try
{
document.write(junkVariable)
}
catch (exception)
{
document.write(exception)
}
The output of the above would be:
‘junkVariable’ is undefined
EDIT: As noted by psychotik's comment, the window.onerror event does not work in Google Chrome. (Source)

Categories

Resources