window.onerror in Safari ( Javascript ) - javascript

I create an error message its working with IE and Mozila.
Not woking with Safari, Chrome and Opera.
But I need to use it. Please give me right way for doing it.
<script language="javascript" type="text/javascript">
window.onerror = function(msg, url, line)
{
document.write("Message\t = "+msg + "<br/>Location\t = " + url + "<br/>Line No.\t = " + line + "<br/>Error No.\t = " + this.err.number);
}
this.err = Error(12,"My Own Error");
throw this.err;
</script>
==========================================
Internet Explorer:
My Error
Message = My Own Error
Location = http://localhost/practice/JavaScript/window.errors.php
Line No. = 8
Error No. = 12
================================================
Mozilla FireFox:
My Error
Message = Script error.
Location = My Own Error
Line No. = 0
Error No. = undefined
=====================================================
Safari, Chrome, Opera:
My Error
look the code Mozilla give wrong information. what I do?

Opera doesn't support window.onerror at all. Chrome supports it, but not on errors that you throw yourself. This is also true of Internet Explorer when using Error objects other than Error(), e.g. TypeError(). Chrome also doesn't provide the line and file arguments.
You should correctly catch any exceptions you're going to throw with a try...catch statement, instead of relying on window.onerror.

Related

Defining a function

At the moment I have the below code on a website
*global escape: true */
var twitterHandle = "Handle";
function tweetCurrentPage() {
return window.open("https://twitter.com/share?url="+escape(window.location.href) + "&text=" + document.title + " via #" + twitterHandle, "", "menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600"), !1;
}
Internet Explorer F12 Debugger at the moment throws up the following error.
The value of the property 'tweetCurrentPage' is null or undefined, not a Function object.
Works fine on Chrome, Safari and Edge but not on IE11 nor Firefox - any help would be appreciated. I have put the code through various online bug checkers and so on. Only error they have come up with is Function is not called... but I call the function in other ways.

FireFoxDriver JavaScript errors does not show in FireFox browser

I'm using the following code to capture JavaScript errors when running Selenium tests:
public static void AssertNoJavaScriptErrorsInLog(this RemoteWebDriver driver)
{
var errorStrings = new List<string> { "SyntaxError", "EvalError", "ReferenceError", "RangeError", "TypeError", "URIError" };
var jsErrors = driver.Manage().Logs.GetLog(LogType.Browser).Where(x => errorStrings.Any(e => x.Message.Contains(e))).ToList();
if (jsErrors.Any())
{
Assert.Fail("JavaScript error(s):" + Environment.NewLine + jsErrors.Aggregate("", (s, entry) => s + entry.Message + Environment.NewLine));
}
}
One of my tests intermittently reports an error:
Assert.Fail failed. JavaScript error(s):
TypeError: doc.documentElement is null
TypeError: doc.documentElement is null
Trying to locate the error I look in the console of the FireFox browser which just ran the failed tests, but it's empty!
Why can't I see the JavaScript error in the browser console?
You might be experiencing the following fresh selenium issue:
Some new browser logs are not captured
As a workaround, consider downgrading selenium to 2.52.
Also, from time to time there are compatibility issues between selenium and firefox with a wide range of symptoms - play around with firefox versions (you can download older versions here) and see if you still cannot catch the js error on the console.

ParseFromString throws error in IE, but not in Chrome

I'm using a KML plugin for leaflet that works great in Google Chrome. In IE, however, It throws an error at the following code.
parser=new DOMParser();
console.log(url) // outputs: "path/to/kmlfile.kml" in Chrome debugger
url=parser.parseFromString(url,"text/xml"); //This line throws a parser error in IE 11, but is fine in Chrome
It seems to me that there is a mistake in this code - the author should pass an actual XML string, not just a url to an XML document to the parser.parseFromString() function. It makes sense that the parser would have an error, as a path to a file is not a valid XML file (Note: kml files are just XML). However, this does not cause any errors to be thrown in the Chrome Debugger tools, which is really strange.
It seems to me that this should fail in both instances. Trusty MDN docs on DOMParser have no mention of putting a URL as a parameter in parseFromString(). So my question is why is this working in Chrome, but throwing an error in IE, and then what can I do to fix it?
Note this question is different from the following url because this isn't a general error - this is about something that works in Chrome but fails in IE: Internet Explorer 11 (IE 11) Throws Syntax Error using parseFromString in DOMParser
When the XML is malformed non-Microsoft browsers (Firefox, Chrome, etc) it will create the XML document with the error message as it's content. Click here (<-- click there).
When the XML is malformed in Microsoft browsers, IE and Edge, it throws an error, writes an error to the console and your script stops. Note I'm on a Mac so I've tested this remotely but have not had a chance to test it personally. You can put that code in a try catch block for IE but what I mean is I don't know if that will stop it from writing a message to the console.
Here's the code pen with intentionally malformed XML and the error message is written in the output. There is no error in the codepen or output. I'm intentionally writing the error code from the parser to the output window. Open the console to see what's going on.
FWIW IE is the correct behavior IMHO. Not throwing errors was the Internet way to do things until relatively recently. The problem with not throwing errors is you don't know what you did wrong or where. Write once, debug everything.
Also, until more recent versions, IE used ActiveX to parse XML documents.
From W3C XML validation script:
function validateXML(text) {
var message;
var parser;
var xmlDoc;
// code for Edge, IE, Mozilla, Firefox, Opera, etc.
if (document.implementation.createDocument || window.DOMParser) {
parser = new DOMParser();
try {
xmlDoc = parser.parseFromString(text, "text/xml");
}
catch (error) {
}
if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
return xmlDoc.getElementsByTagName("parsererror")[0];
}
else {
return "No errors found";
}
}
// code for older versions of IE
else if (window.ActiveXObject) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
try {
xmlDoc.loadXML(text);
}
catch (error) {
}
if (xmlDoc.parseError.errorCode != 0) {
message = "Error Code: " + xmlDoc.parseError.errorCode + "\\n";
message = message + "Error Reason: " + xmlDoc.parseError.reason;
message = message + "Error Line: " + xmlDoc.parseError.line;
return message;
}
else {
return "No errors found";
}
}
else {
return "Not supported";
}
}
Related question.

Retrieving the line number from an Internet Explorer error object

Is there a way to retrieve the line number from an Internet Explorer 7/8 error object?
I'm only aware of the .message, .description and .number properties.
I've searched around a bit and found an MSDN article on .stack ( http://msdn.microsoft.com/en-us/library/hh699850(v=vs.94).aspx ), but even using their own example code doesn't return a line number:
http://jsfiddle.net/LWevS/
I dug around some more and found that it is possible to retrieve the line number in IE using window.onerror. It's not from the Error object itself, but it's a decent workaround:
function BadFunction(){
This.badcode.willnot.work = 1000
}
function ForceError(msg, url, lno) {
alert("Error Occurred! Handled by Generic Error Handler" + "\n" +
"Error: " + msg + "\n" + "URL: " + url + "\n" +
"Line Number: " + lno);
return true;
}
window.onerror = ForceError;
This method requires that the error bubble up to the window. If you have a try/catch in your code, you will need to re-throw the error so it can bubble up.
I also came across StackTraceJS on GitHub while researching a solution to this problem. Their stack tracing script works great on all browsers except for Internet Explorer. A great run-time debugger if you want to log errors produced on the client browser.
http://stacktracejs.com/

How do I detect if ActiveX is enabled in the browser of client?

How do I detect if ActiveX is enabled in the browser of client?
I tried following code, but it's not working in Firefox.
window.ActiveXObject not working in Firefox
any ideas?
check the example here: http://jsfiddle.net/qXSvQ/2/
I get false when I run this example.
ActiveX objects do not exist in anything but Internet Explorer. If you're trying to use them for XMLHTTPRequests, use the XMLHTTPRequest() object instead, using feature detection.
if ("ActiveXObject" in window) { /* Do ActiveX Stuff */ }
else { /* ActiveX doesnt exist, use something else */ }
What isn't working? Is that throwing an error in FF? How about
var hasAX = "ActiveXObject" in window;
Below code should work, It is working on IE6 & FF 3.6.12 atleast.
if(typeof(window.ActiveXObject)=="undefined"){
alert("ActiveX Object not supported");
}else {
alert("ActiveX Object supported");
}
It seems Firefox simply skips scripts containing ActiveX objects:
<script><!--
var activeXsupport = "ActiveX not supported";
// --></script>
<script><!--
var dummy = new ActiveXObject ('WScript.Shell');
activeXsupport = "ActiveX supported";
// --></script>
<script><!--
alert (activeXsupport);
// --></script>
So this gives me "supported" on IE11 and "not supported" on Firefox.
[Edit:] Since it also throws an error message on Firefox if the console is opened with [F12], I suggest this improvement:
<script><!--
var dummy = ''; var hasActiveX = false;
try {dummy = new ActiveXObject ('WScript.Shell'); hasActiveX = true;}
catch (err) {dummy = ''; hasActiveX = false;}
alert ('hasActiveX = ' + hasActiveX);
// --></script>
Edge Chromium supports ActiveX if it is made the default browser in settings and reloading in Internet Explorer Mode via "More Tools" is enabled:
edge://settings/defaultBrowser
Gerolf

Categories