How can i make IF in javascript false?

I m trying to block certain countries in my website.

This script will only load my website in the US, CA(Canada) and GB(Great Britain) and the alert will show if you're accessing in other countries.

<script language="JavaScript">
var geo = disp();
if (geo[0] != 'US' && geo[0] != 'CA' && geo[0] != 'GB'){
   alert('Sorry this site is only accessible from the USA, Canada 
          and Great Britain');
   window.location = ('http://www.yahoo.com');
}
</script>

But I want to show the alert on a particular country.

What is the opposite of IF..?

Or How can I make the statement false..?

Answers


Im trying to block a certain country in my website

if (geo[0] == 'The country') {
    alert('Not allowed');
    window.location = 'http://google.com/';
}

You don't need an else if you just want to block one country.


Blocking a site in Javascript ?

Do you realize how useless this is, you can only block from a backend using IP or whatever. But client side this is useless. Visitors just have to disable JS and tada! there are on your website.

So what you could do it use a library on your backend using geoip's database to block accoring to ips.

Here is one way to do it with iptables. And here directly into Apache.

Blocking by IP is not perfect but that is as good as you can do it.


Just use "else":

<script language="JavaScript">
var geo = disp();
if (geo[0] != 'US' && geo[0] != 'CA' && geo[0] != 'GB'){
   alert('Sorry this site is only accessible from the USA, Canada and Great Britain');
   window.location = ('http://www.yahoo.com');
} else {
   // some other stuff
}
</script>

just

if(){
   // if the condition in if is true  
}else{
  // if the condition of if false
}

or use the ! (NOT) operator

if(!condition){
   // if the condition in if is false
 }

and the blocking of other site by JavaScript is not a good even worse idea to choose . it should be done at server side


The 'opposite' of if is else.

 if(condition) {
   [This gets executed if condition is true]
}
else {   //or if(!condition)
   [This gets executed if condition is false]
}

You need to look over some basic javascript tutorials before posting questions like this.


If a client scripts are disabled on a browser then your code won't work at all..So most probably you have to change your logic or strategy..

What You can do is:

You can check that via IP Addresses.


Need Your Help

Possible to have several instances of Eclipse open simultaneously?

java eclipse

I'd like to know if it's possible to have several instances of Eclipse open at the same time. I'm using it for Java development. I know I can have several files from different projects open at the ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.