show line numbers for console statements in IE 9 debugger - javascript

I hope this is something simple..
I am using the F12 Development tool debugger in IE 9. Is there a way I can display the line numbers and file source for each console statement, the same way that Firebug displays this info? I may have overlooked something basic, but I haven't yet found a way to do this..Thanks!

The only way to get line numbers is to define a window.onerror method:
window.onerror = function (message, url, lineNo)
{
console.log('Error: ' + message + '\n' + 'Line Number: ' + lineNo);
return true;
}
console.log(window);
console.log(1=2);
But even then, IE breaks on the error, so it's rather useless.

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.

Strange javascript bug

Pls look at the following code:
html...
<pre id='output'></pre>
html...
JS
function log(text) {
var div = document.getElementById('output');
div.innerHTML += text + '\n';
}
function foo() {
var browser=0;
if(navigator.appName.toUpperCase()=="MICROSOFT INTERNET EXPLORER")
browser=1;
else
if(navigator.appName.toUpperCase()=="NETSCAPE")
browser=2;
log ('browser:'+browser);
if (browser==1)
{
log ('IE');
}
if (browser==2);
{
log ('Chrome');
}
if (browser==0);
{
log ('Could not determine broweser type');
return;
}
}
When I run this from IE the output is:
browser:1 ie not supported Chrome extension will be loaded Could not determine broweser type
When I run it from Chrome the output is:
browser:2
Chrome extension will be loaded
Could not determine broweser type
How can it be that browser has more than one value?
10xs,
Nir
You have a very beginners mistake in your code
The ; at the end of the if
if (browser==0); causes your if condition to end and the rest is a normal code block which gets executed every time no matter the value of browser
You should not put ; after an if statement: if (browser==2);

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/

Internet Explorer 9 in compatibility-mode shows error

While testing my jQuery script in IE 9 it shows error in jquery-1.6.2.js --
SCRIPT5022: Exception thrown and not caught
jquery-1.6.2.js, line 548 character 3
My js file used to work with no problems in other browsers - firefox 3.6, 5.0, opera 11, chromium, chrome. I do not understand. Is there a bug in the jquery source or is there a bug in my js file ? How do I find it ?
It hasen't been long since I started in this area so please I would really appreciate some ideas.
Thank you in advance.
[edited]
I've added the complete code.
I've learnt from :
http://net.tutsplus.com/tutorials/javascript-ajax/how-to-create-a-jquery-image-cropping-plug-in-from-scratch-part-ii/
I don't know which part to add. The whole code does not fit in stackoverflow's submit. says "body is limited to 3000 characters; you entered 42121 "
[2nd edit]
I have used 'error' in my ajax submit.
//jquery ajax submit for resizing
function submit(myform,e){
$n.val($image.attr('src').replace(/^[^,]*\//,''));
var mydata = myform.serialize();
$.ajax({
url: $myform.attr('action'),
cache: false,
type:$myform.attr('method'),
data: mydata,
success: function(response){
var img = $("<img />").attr('src', 'images/'+response+'?'+ e.timeStamp) //<-- img name here
.load(function() {
if (!this.complete || typeof this.naturalWidth == "undefined" || this.naturalWidth == 0) {
alert('Incorrect image.');
} else {
$("#result").html(img); //<-- put cropped img here.
}
});
},
error: function(){ //<-- error used here
$('#result').html("Err. Loading Image");
}
});
};
I traced out my problem. IE prevented changing of attribute which let to the jQuery error.
Note: jQuery prohibits changing the type attribute on an or element and
will throw an error in all browsers. This is because the type attribute
cannot be changed in Internet Explorer."
from http://api.jquery.com/attr, just after 1st warning notice.
So I changed my code from earlier
$('input').clone()
to
$('<input />').attr({'type':'hidden'})
and it solved my problem.
Thanks everyone.
jQuery.error() (which throws the message passed as an argument), is totally different to the error handler specified in an AJAX call.
jQuery.error() is used internally by jQuery (search for "error("). You should debug your code and follow the stack trace to find which of your methods is calling the jQuery method which is erroring.
Just a guess, but do you somewhere use $.error() inside your code? If yes: don't use it.
<edit>
However, this error comes from jQuery.error().
The original error-function looks like this:
error: function( msg ) {
throw msg;
}
You may test it without jquery:
(function( msg ) {
throw msg;
})('errormessage');
You'll get the same error.
If you don't use jQuery.error() maybe you use some plugin that uses it.
You may override this (buggy?) function by executing the following right after the embedded jquery.js:
jQuery.error=function(){}
</edit>

window.onerror in Safari ( 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.

Categories