Script stack space exhausted firefox - javascript

I am working with a large XML response from a web service. When I try to get that using a URL, after some time it displays an error in Firebug that "script stack space quota is exhausted"
How can i resolve that?

It sounds like there is some recursion going on when processing the xml, that is essentially causing a stack overflow (by any name).
Thoughts:
work with less data
if you are processing the data manually, try to use less recursion? perhaps manual tail-call or queue/stack based
consider json - then you can offload to the script host to rehydrate the object without any extra processing

Have you tried disabling Firebug?

As of Firefox 3, the available stack space has dropped from 4MB to ~= 640KB (I'm passing on word of mouth here).
Do you happen to be running FF3?
https://bugzilla.mozilla.org/show_bug.cgi?id=420874

I had a similar problem, maybe the same.
This can happen if you try to parse a huge chunk of html with jQuery $(html).
In my tests this only happened on Firefox 3.6.16 on Windows.
Firefox 4.0.1 on Ubuntu behaved much better. Probably nothing to do with the OS, just the script engine in 4.x is much better..
Solution:
Instead of
var $divRoot = $(html);
I did
var $temp = $('<div style="display:none;">'); // .appendTo($('body')); // (*)
$temp.html(html); // using the client's html parsing
var $divRoot = $('> div', $temp); // or .children() or whatever
// $temp.remove(); // (*)
(*)
I remember that in some cases you need to add the temp node to the body, before jquery can apply any selectors. However, in this case it seemed to work just fine without that.
There was absolutely no difference on FF 4.x, but it did allow to avoid the stack space overflow error on FF 3.x.

Related

JSON Stringify crashing

I've been testing some code which takes a variable in a json format and should print that, it prints an empty array however.
If I'm trying this:
console.log(JSON.stringify({first:1,second:2}));
Then I'm crashing the page (Chrome: "Aw, Snap!").
I've asked a few people, and they weren't able to reproduce it, I however get it every time. Tested it in FireFox too, and there it crashes too.
This was the code:
var timer={first:0,second:0,third:0,fourth:0};
localStorage.setItem('saveTimers', JSON.stringify(timer));
And that sets [] in the localStorage
I was able to crash it when running it this a lot:
for (var i = 0; i < 100000; i ++) {
var timer={first:0,second:0,third:0,fourth:0};
localStorage.setItem('saveTimers', JSON.stringify(timer));
}
Perhaps you're running this code a bunch of times really quickly? A solution to this might be to throttle your function, which can be done by implementing a throttle function or with Underscore.js's throttle.
So by my understanding the problem you're really having is saving things to localStorage.
This seems more like a permissions problem than anything else. Does the browser have permission to access the filesystem? Are the folders used by the browser available for writing? Have you tried re-installing the browsers?
These are the things you should be checking.
If you are using a beta or dev build of chrome, double check that this behaviour is the same in a stable build and if not, then there's your problem.

IE not loading page with Javascript and Raphael

I'm testing out a website that runs fine on Firefox (Win/Mac), Chrome (Win/Mac) and Safari. I'm having difficulty with Internet Explorer unfortunately. I get the following error message:
SCRIPT65535: Unexpected call to method or property access.
raphael-min.js, line 8 character 64961
I've taken a look at the debug output which looks like just takes me to a part of the Raphel library:
c=a.getScreenCTM()||a.createSVGMatrix()
I've searched for this error message online, but I don't understand what solution is relevant to this case as I've no idea what is causing the problem. I am also using the jQuery library. Are there any tests that I can do that can give me more information about the source of the problem?
I just found how to patch this, in order to keep the compressed version of Raphael.
Replace (don't forget the coma):
c=a.getScreenCTM()||a.createSVGMatrix(),
By that (dont't forget the end space):
c;try{c=a.getScreenCTM()||a.createSVGMatrix()}catch(e){c=a.createSVGMatrix()};var
Works fine ! :)
Means :
c; : declaration of variable c, and stop the first instruction.
try{c=a.getScreenCTM()||a.createSVGMatrix()}catch(e){c=a.createSVGMatrix()}; : our instruction, surrounded by a try/catch, to avoid IE error
var + a space : (don't forget the space!) allow us to continue to declare variable
I found out that it's an issue with compression (of the js file). I had the exact same issue and I had been searching for a solution. Guess what? I tried it with the uncompressed Raphael file and voila! No more issues. Compressed file needs a tweak, it seems.

Javascript testing resource

I have a development site which seems to have an intermittent Javascript error in IE7. Now I can fully test IE7, but I was wondering if there is a resource that would allow me to analyze the Javascript for possible browser issues.
You can try running your javascript through JSLint
A common IE-only problem is the inclusion of closing commas in array or object literals. Non-IE browsers handle this fine.
Are you generating any array or object literals via a JSON response or similar?
Eg
a = [1,2,4,]; // error in IE, not in other browsers
b = {a: 1, b: 2,}; // also error in IE
Look for any code that runs as the page loads.
ex:
<script src = "test.js"></script>
test.js
document.getElementById("whatever");
Depending on the speed of the JS interpreter, you'll get a race condition with the loading of the script vs. the loading of the DOM. If the issue is "intermittent", this is almost certainly the problem. Stick everything that looks at the DOM in $(document).ready
You may wish to try using Selenium for scenario tests as well.

Find IE-breaking ECMAScript/JavaScript errors

I work on a relatively huge ECMAScript codebase (> 60,000 LOC) and we tend to struggle a bit with detecting errors for our dreaded friend Internet Explorer (especially 6 and 7).
At the moment, I've been stuck with an issue for 3 days where my RIA renders fine in Google Chrome, Firefox 3.6, Opera and Internet Explorer 8, but fails when running Internet Explorer 8 in IE7-Mode (or with a real IE-7).
My question really is: how do you do to identify code that will produce an error in IE7?
Usually I rely on JSLint and I tend to catch the usual suspects (trailing commas, I loathe you), but in this particular case I have just re-run a linter on all my code, both source and minimized, and it doesn't yield my usual suspects. So I guess I have mistakenly introduced something that IE doesn't like (who knows, maybe I got crazy and used Array.map instead of dojo.map somewhere?) and it blows up in my face, producing nice error messages that don't help me at all ("[object object]" and " is null" where it shouldn't be, so I assume there was an error up-stream that failed silently and prevented this object from being created).
I've tried having a look at the Google Closure Linter but it doesn't yield anything special, and I don't think that the Google Closure Compiler is going to be my savior either. is there any tool (command-line, web-service or other) that can parse/run code as if it were emulating IE so we can get the appropriate errors?
Any tips are appreciated.
EDIT: Thank you for your help in trying to solve my issue so far, but what I am really just asking is if there are tools that do this sort of checks, meaning validating a feature set and syntax against a particular browser. This is the sort of thing severly lacking in the JS-world in my opinion (not so much for FF or Chrome, obviously as their debuggers are a bit more helpful).
EDIT2: I eventually found the root of my problem today (after 3 days) by going through all my code changes between two branches and realizing that the problem was actually already there but never detected before and going back through even older changes to narrow down the mess and eventually end up adding console logs everywhere until I reached a point of failure (God thank emacs for its regular expression support to add logs to every single line... heavy but works...). Fun fact though: IE swallowed an error message supposed to be displayed in the try catch block originally catching the source problem and then re-throwing it. Still don't why, but if it hadn't then that would have been a lot easier to find, as it was intended for that purpose in case it broke. Weird. Might not like deep-levels of re-throw.
I'll leave the question open as for now there are no answer to the actual question.
You might try the IE8 debugger, as #galambalazs suggests; the old debugger from the IE6 era was generally not useful.
The low-level technique I've always used is to add my own try/catch blocks around large portions of the my Javascript source to narrow down the source of error. By iteratively adjusting your try/catch blocks you can do a "binary search" through your source to locate the code that's causing an exception. What you'll probably find is that there's some code that's harmless in Firefox but which IE's interpreter considers an error. (To be fair, it's usually the case that IE's strictness is justified, and that the lax behavior of Firefox is really undesirable.)
So in other words, you'd start on a Javascript source that you suspect, or perhaps you'd do this to all of your included .js files:
// top of Javascript source file foo.js
try {
// ... all the code ...
} catch (x) { alert("Error in foo.js: " + x); }
Now, if you load the page and get that alert, then you know that the error is somewhere in foo.js. Thus, you split it in half:
// top of foo.js
try {
// ... half the code, roughly ...
}
catch (x) { alert("Error in first half of foo.js: " + x); }
try {
// ... the rest of the code ...
} catch (x) { alert("Error in second half of foo.js: " + x); }
Repeat that process and you'll eventually find the problem.
You may try Microsoft Script Debugger, or Internet Explorer Developer Tools.
For complete list of what may be different in IE 8 from the older version consult with:
Internet Explorer 8 Readiness Toolkit (offical list)
especially DOM Improvements
JavaScript in Internet Explorer 8 (John Resig's post)
Also see Web Bug Track for possible quirks.
As a final note, doing console.log on every line may help you to find a particular bug in a hard way, but considering the amount of code you should really write unit test for modules. That's the only way you can make sure that your application will actually run with different inputs and conditions.

on a javascript error, how to identify method or js file with the problem?

When a javascript error occures in IE (or in other browsers) you get a popup saying that javascript error has occurred - usually this comes with a line number and some hint.
Sometimes it comes with line 0 and with no way of knowing what the problem is.
Javscript can come from HTML itself, from a js file or from JSP (and more).
Microsoft has a script debugger that helps a lot in finding where js errors are, however sometimes when a js error occurs the script debugger cannot find the code portion and thus its difficult of finding where is the root cause of the problem.
My question is whether anyone knows any way of making script debugger find the code any way (mostly happen with js code that is in JSP file), or at least include in the IE popup the method or js file where the error has occurred. (it only display the line number, and many times its line 0...).
Thanks,
Tal.
The error object which is created when an error is thrown by JavaScript is very unreliable when it comes to the source line, especially within IE. Browsers like Firefox and Safari are better at line numbers, but they are generally pointless due to minification of the files.
What is obviously of more use is getting the call stack, but due to the anonymous nature of JavaScript functions (well, that they can be anonymous) a call stack can often be hard to work out.
If you're doing a try/ catch you can do arguments.callee which will return you the method which called the current method which failed.
Here's a good example of doing a complete stack in JavaScript - http://eriwen.com/javascript/js-stack-trace/
Also developer tools included with Internet Explorer 8 is something good to trace and debug your javascript code
There is a version of Firebug called Firebug Lite that will work with Internet Explorer. It's performance is going to be based on how complex your pages are; however, for relatively lightweight pages, it should provide some insight.
I recommend this tool rather than simply using Firebug and Firefox because not all errors that occur in Internet Explorer will occur in Firefox, and so performing any debugging in that browser may not yield any results.
Firebug on Firefox is usually considered one of the best debugging tools.
On Firefox, go to
http://getfirebug.com
to get it.
This will print you a stack trace:
function Stack()
{
try
{
throw Error()
}
catch(ex)
{
return ex.stack
}
};
print( Stack() );
If all else fails (and when dealing with IE it sometimes does) you can always walk through your code with alerts. It's crude and tedious, but sometimes it's all you can do:
Simply:
var count = 0;
then sprinkle some:
alert(count++);
at strategic lines along your code and note where it stops alerting.
Lather rinse repeat until you have your line.
If using Firefox you can press Ctrl + Shift + J to bring up the JavaScript error console that is built into Firefox, which will tell you exactly what went wrong.

Categories