Вопрос

I'm making a model viewer online with Javascripts. For security reason, i don't want user can run it anywhere out of my site.

I already obfucated and lock domain but if user download whole website, they still can run offline.

So, how to detect that user is running offline and stop working by Javascript?

Thanks.

Это было полезно?

Решение

var checkAfter=15; //15 seconds
  setTimeOut(CheckNavigatorState,checkAfter*1000);

function CheckNavigatorState(){
   if(navigator.onLine){
       //  --- Add javascript src OR call js functions here
   }else{       
      //----remove javascript source OR stop functions    
    setTimeOut(CheckNavigatorState,checkAfter*1000);
   }
}

I would like to mention that it is not easy to tell whether the browser is offline or not. Some browser vendors say the browser is offline when computer loose its connection to the network, which is not really exact. We know that you can have access to the LAN but not to the internet. So to do what you may need to ping a real distant server, such as Google.

EDITS: To ping with JavaScript, check this fiddle (edited)

Другие советы

You could make the javascript code check the domain it is running on using the window.location property (http://www.roberthalf.com/technology/External_Sites/content/RHT-NA3/Shared/Images/logos/rht_logo_c.gif) but that will only keep people who don't know how to modify javascript code from running it locally. The better solution would be to make your code dependent on something on the server so that whenever they try to do something important it makes an ajax call to the server, which can then check the referrer and return an error if they aren't running from the server.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top