-3

Hello Guys I would like to know if it is possible to hide the div if the internet is not availaible. Here is the code

if(navigator.onLine) { 
     //show
}
else{
//Hide
} 

Thanks in advance.

mark22
  • 17
  • 7
  • 1
    See here https://stackoverflow.com/questions/189430/detect-the-internet-connection-is-offline – Huangism Jan 25 '18 at 19:25
  • Read https://developer.mozilla.org/en-US/docs/Web/API/NavigatorOnLine/Online_and_offline_events – charlietfl Jan 25 '18 at 19:26
  • @Huangism no help. How do I hide div element? – mark22 Jan 25 '18 at 19:27
  • 1
    @mark22 have you asked google? while I didn't vote you down but your question clearly shows lack of research or attempts made to solve the issue. Just google how to hide div with jquery – Huangism Jan 25 '18 at 19:28
  • 2
    Please spend the time to read [ask] and [mcve]. This is another of your questions that simply does not provide enough details or a specific enough problem statement. Of course you can do what you ask and the first obvious question would be what went wrong when you tried – charlietfl Jan 25 '18 at 19:28

2 Answers2

0

You can check whether the internet is connected or not by

window.onload = checkInternetConnection;
function checkInternetConnection() {
  var isOnLine = navigator.onLine;
   if (isOnLine) {
      alert(connectionMessage);
   } else {
     alert(noConnectionMessage);
   }
}

so if isOffline, You can use jquery to hide that specific div like

$('.div_class_name').hide();

and if online, use

$('.div_class_name').show();
Sujan Gainju
  • 4,273
  • 2
  • 14
  • 34
0

You can use JQuery.

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

Code:

    $(document).ready(function(){
      if(navigator.onLine) { 
           $("#element-id").show();
        }
        else{
          $("#element-id").hide();
        } 
    });.