Abort Google Analytics when I print if there is no internet
I Couldn't find specifically what I need on the net. The problem is:
When a user's machine doesn't have an internet connection and tries to load Google Analytics the page waits for about 1.5 minutes depending on the natural time out of the browser.
This is a problem for a network that prohibits connecting to Google and only allows connection to the web server. There is a good chance that we will cater to a client with this set-up.
Now while the user is waiting for a reply from www.google-analytics.com/analytics.js and the user presses our print button, which does some css stuff to create a print format of the page then calls a Javascript window.print(), the browser now waits for the reply of Google Analytics before showing the print panel. Tested on Chrome and Firefox.
I'm looking for way to abort the request if it is still loading when the print button is pressed.
Google Analytics snippet.
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXX-Y', 'auto'); // Creates a tracker.
ga('send', 'pageview'); // Sends a pageview.
<input id="clickMe" type="button" value="print" onclick="print();"/> function print(){ window.print() }
I tried to convert it to the old Google Analytics and added an abort call to the ajax function variable, however, it still did not abort.
You could try disabling all pending requests when executing your print
callback by using window.stop()
. According to MDN:
The stop() method is exactly equivalent to clicking the stop button in the browser. Because of the order in which scripts are loaded, the stop() method cannot stop the document in which it is contained from loading, but it will stop the loading of large images, new windows, and other objects whose loading is deferred.
Unfortunately, it seems that older versions of Internet Explorer do not support this method, so you might also need to use document.execCommand('Stop');
.
This article found online gives more details about this feature: http://hungred.com/useful-information/opera-netscape-issues-stop-page-loading-javascript/
Hope it helps!
链接地址: http://www.djcxy.com/p/31188.html