Answer the question
In order to leave comments, you need to log in
Help with JavaScript
Comrades. Help me with JavaScript.
The essence of the problem is this. There is a code:
This is the code for my site from Google Analytics. I should place this code before the closing [/head] tag. With that, I dealt with it.
So I need this code NOT to be executed when a user from my local network visits the site . Those. with addresses from the range 192.168.0.1 - 192.168.200.254 (because not everyone has the Internet, and those who do not have the site start to slow down terribly).
Help out.
script type="text/jаvаscript"
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-ТУТНАБОРЦИФЕРОК']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
/script
Answer the question
In order to leave comments, you need to log in
Using server-side tools, display a block of code depending on the value of the REMOTE_ADDR server variable, which contains the IP address of the computer that requested the page.
The simplest solution to the problem is to simply write in the place where you paste the script
. This will solve the problem for all modern browsers.
1. Hang the code on the document ready event
2. Ping the Internet with an asynchronous request with a low latency
3. If everything is ok, execute your code
As you have already suggested above, perhaps.
And you can ping easily and naturally by connecting jQuery, say:
$.ajax({
'url':'http://ya.ru',
'success': function(data){
//Ваш код
}
});
As an option, make a second domain for such users and check
(function() {
if (location.host == "vtoroi.domain.ru"){
return;
}
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question