Page refresh in case of javascript errors - javascript

I have some pages running javascript for displaying updates of different measure values. These stuff seems to run fine. But - there is always the risk that javascript can crash in case of a fault (especially after running some hours)
So I want to implement a simple kind of watch dog. One of my ideas is to use a meta-refresh-tag. The Browser will reload the site after xy minutes and all javascripts will be reinitalized.
Of course I do not want to refresh the site if no error occured and want to reset the refresh timer using javascript. As long js runs the timer will be resetted periodally. If javascript crashs the meta-refresh-timer counts down to zero and the page will be reloaded.
Reading stackoverflow postings I have found some answers that says a reset of the meta-timer by javascript is not possible. Do you know a better way to implement the watchdog? Using javascript ittself for refreshing is useless: If the script crashs it will never fire a reload event..

Ideally you should properly handle the error instead of just reloading the page. However you could use the window.onerror event like this:
window.onerror = function() {
location.reload();
}
More information about onerror here:
https://developer.mozilla.org/en/docs/DOM/window.onerror

Related

Is it possibile to stop a script from another script?

I started to make a simple js debugger, and I was wondering if there is a way to stop an already running script if either it isn't responding or I want to stop it's setInterval() loop where we don't know the id of the loop.
I've noticed in the Chrome DevTools that every document has it's own script collection under document.scripts, but I couldn't find anything like a list of intervals (eg:window.intervals) or any response if any function is running,so I can't really determine if a function needs to be killed.
This could be useful when you want a webpage scanner extension or a debugger thet can refresh the page when the script is frozen.
Edit:
The list of intervals has already been asked and solved in How can I get all timers in javascript question.
Now the only question remains is: How can I determine if a script is frozen so it or the page can be killed?

Save and load entire web page state (Visual HTML, And javascript Variables)

So lately I've been working on this project, which will end at the end of January (end of this week).
http://www.nikollr3.three.axc.nl/ (this is a preview of the program a few months ago, but u get an idea)
We have started testing the website and noticed that it was sometimes making Firefox crash.. since Firefox' error or crash reports were never of any use (no useful information found), I have no idea how to fix this error. And a possible solution would be to have a save / load button on the web page, which can save or load a state (and for example autosave state untill it crashes) then when it crashes you would be able to reload the last state and continue working on the project. By saving a 'state' I mean recreating the EXACT moment as to when the save button was clicked, thus same javascript variable values and same HTML looks.
I have found little things about this on the internet, the only one relevant being for android phones,.. Any ideas or implementations?
write all your variables and "HTML looks" (attributs) to a XML-file. load (if (exist(XML)pseudo-code) all necessary data onPageLoad

Programatically retrieve count of javascript errors on page

I'd like to write a test case (using Selenium, but not the point of this question) to validate that my web application has no script errors\warnings or unhanded exceptions at certain points in time (like after initializing a major library).
This information can easily be seen in the debug consoles of most browsers. Is it possible to execute a javascript statement to get this information programatically?
It's okay if it's different for each browser, I can deal with that.
not so far read about your issue (as far as I understood your problem) here
The idea be the following:
I found, however, that I was often getting JavaScript errors when the page first loaded (because I was working on the JS and was introducing errors), so I was looking for a quick way to add an assert to my test to check whether any JS errors occurred. After some Googling I came to the conclusion that there is nothing built into Selenium to support this, but there are a number of hacks that can be used to accomplish it. I'm going to describe one of them here. Let me state again, for the record, that this is pretty hacky. I'd love to hear from others who may have better solutions.
I simply add a script to my page that will catch any JS errors by intercepting the window.onerror event:
<script type="text/javascript">
window.onerror=function(msg){
$("body").attr("JSError",msg);
}
</script>
This will cause an attribute called JSError with a value corresponding to the JavaScript error message to be added to the body tag of my document if a JavaScript error occurs. Note that I'm using jQuery to do this, so this specific example won't work if jQuery fails to load. Then, in my Selenium test, I just use the command assertElementNotPresent with a target of //body[#JSError]. Now, if any JavaScript errors occur on the page my test will fail and I'll know I have to address them first. If, for some strange reason, I want to check for a particular JavaScript error, I could use the assertElementPresent command with a target of //body[#JSError='the error message'].
Hope this fresh idea helps you :)
try {
//code
} catch(exception) {
//send ajax request: exception.message, exception.stack, etc.
}
More info - MDN Documentation

Google Analytics: _trackPageview without page refresh?

I'm reworking some site tracking for a site I'm working with. For the tracking we are currently using Google Analytics, which seems to be working fairly well. However, I'm having some troubles resembling the ones in this question, but it's old and no one answered, so I'm bumping a bit here. :)
Basically, I'm tracking two kinds of things. Raw pageviews (entering a page), and events on the page (lightbox opened, something important clicked, etc). I'm using _trackPageview for both kinds of events, because I need to be able to track some lightbox flows in GA's goal funnel tracking, and as I understand it _trackEvent calls can't be tracked in goal funnels.
The problem here is that it seems like the way GA works, it doesn't really post its data instantly (firebug doesn't show any requests happening, at least), but defers it to a page refresh or something like that. I'm not totally sure what happens, but basically I'm getting all events up to the first one leading to a page refresh all shuffled up in the funnel and looking like they all happened as an exit from the event causing the refresh. (Did that make sense? :) Is there any way of forcing GA to "flush" an event when it happens and not defer it? Or am I using things totally wrong?
EDIT: I was a bit blind reading the firebug logs... It does actually do the request to __utm.gif with the correct data. Makes the funnel being weird even more strange though, so the basic question is still valid.
Thanks
I made a function for this. We wanted to track how many people click on each on of a few links we have so we "track pageviews" for it.
function trackPV(trackerCode, url)
{
var tracker = _gat._getTracker(trackerCode);
if(url)
{
tracker._trackPageview(url);
}
else
{
tracker._trackPageview();
}
}
Basically, you pass in your tracker code (UA-XXXXX) and a url if you'd like to, such as "http://www.example.com/link1", by default it just tracks the page you are on.
Hope this helps.
I believe each call to _trackPageview will submit a unique request to Google Analytics (via parameters to the __utm.gif object). Google Analytics is pretty tough to debug since there is such a lag between the time your send your data, until it is actually visible online. Typically, you will have to wait 4+ hours before your data will show up - so maybe you just need to wait to confirm that your code is working.
Hmmm... I really only have experience with the old GA, but it seems to me that your best course of action is decoding the utm.gif request and seeing if it contains incorrect information. Here's a list of debugging tools that Google recommends.
use "event tracking" . At least check it out in google analytics help.

jquery script is using to much memory

I have the code below on a social network site, it loads a file that shows users new notification messages if they exist.
Problem is when I leave the page open for a while, it eventually consumes too much memory on my PC and starts to make firefox non-responsive and throws an error message saying that memory is getting low and asking if I should abort the script or something along those line.
Is there a way to do what I have it doing but without using up so much memory over time?
<script type='text/javascript'>
$(document).ready(function(){
var updatenotification = function(){
$('#notificationcontainer')
.load('http://localhost/member/beta_new/notifications.inc.php')
.fadeIn("slow");
};
var auto_refresh = setInterval(function(){updatenotification();}, 60000);
updatenotification();
});
</script>
What exactly do you return. I'll assume it is a large ish dom fragment. You could change the code to just return a small json string, check if there is a valid new notification and then write some markup into the div rather than reloading it each time regardless if there are new notifications. Can you maybe show the markup that you are returning.
I am making a few too many assumptions without knowing exactly what your returning. If it is just a tiny html fragement I cant really see the issue. The script looks ok and should not be causing the issues you speak about.
Also remember that firebug does have big issues at the moment. There are some pretty nasty leaky bugs that cause the memory warning dialog when browsing heavyish ajax/js sites like StackOverflow, Netvibes. Your average user (I assume!) will not have firebug so will not see anything

Categories