0

I have two files, a HTML and JavaScript file, in the html i included the twitter.js in the header, and I set id for the div below as following. Then in the twitter.js I wrote the if statement to check for connection, if yes, it loads the twitter function else it writes No Connection in the tweets div, but I can't get it work. Plus, the in the tweets div is loading anyway, how can I include it in the JavaScript file? HTML:

<div id="tweets">
  <a class="twitter-timeline" href="https://twitter.com/user" data-widget-id="id">Tweets by @user</a>
</div>

JavaScript:

if(navigator.onLine) {
  !function(d,s,id){
    var js, fjs = d.getElementsByTagName(s)[0], p=/^http:/.test(d.location)  ? 'http'  : 'https';
    if (!d.getElementById(id)) {
      js = d.createElement(s);
      js.id = id;
      js.src = p + "://platform.twitter.com/widgets.js";
      fjs.parentNode.insertBefore(js,fjs);
    }
  }(document,"script","twitter-wjs");
} else {
  document.getElementById('tweets').innerHTML = '<p>No Connection</p>';
}
Marcelo Aymone
  • 293
  • 3
  • 19
Ali Zahr
  • 64
  • 3
  • 16

5 Answers5

1

Which browser are you using ? The navigator.onLine property shows if the navigator is in online mode or offline mode. It does not shows if you actually have an active internet connection or not.

For example, in Firefox, you have to set the browser in offline mode manually, which means that even though your internet connection is off, navigator.onLine will have the value true unless you switched your browser to offline mode. However, a browser like Google Chrome automatically switches the browser to offline mode if no internet connection is detected, in that case your code should work.

Hope this helps.

EDIT:

What you can do is load the library, and if not loaded then you know you're offline:

HTML

<script src='//platform.twitter.com/widgets.js'></script>

JS

if(!window.twttr){ 
  document.getElementById('tweets').innerHTML = '<p>No Connection</p>'; 
}

The variable window.twttr being available only if the library is properly loaded.

brian
  • 802
  • 4
  • 18
0

what if you empty the #tweets div, and include this line in js when navigator.onLine is true

document.getElementById('tweets').innerHTML = '<a class="twitter-timeline" href="https://twitter.com/user" data-widget-id="id">Tweets by @user</a>';

Let me know if it's useful.

Joaquín O
  • 1,431
  • 1
  • 9
  • 16
0

What you could maybe do is wrap an AJAX call to jQuery in a function and if it returns successfully then you know the user is connected to the internet. Something like this:

var isOnline = false;
function isOnline(){
    $.ajax({
        async:false,
        url: "//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js",
        success:function(){
            online = true;
        },
        error:function(e){
            console.log(e);
            online=false;
        }
    }); 
}

See this jsFiddle for a working example.

tylerargo
  • 1,000
  • 10
  • 13
0

Place the JavaScript code below your <div> element and it should work as it did for me.

javascript

function CheckOnline() {
  if(!navigator.onLine) {
    !function(d,s,id){
      var js, fjs = d.getElementsByTagName(s)[0], p=/^http:/.test(d.location)  ? 'http'  : 'https';
      if (!d.getElementById(id)) {
        js = d.createElement(s);
        js.id = id;
        js.src = p + "://platform.twitter.com/widgets.js";
        fjs.parentNode.insertBefore(js,fjs);
      }
    }(document,"script","twitter-wjs");
  } else {
    document.getElementById('tweets').innerHTML = '<p>No Connection</p>';
  }
}

html

<!-- somewhere in the head -->
<script src="script_name.js"></script>

<!-- somewhere in the body -->
<div id="tweets">
  <a class="twitter-timeline" href="https://twitter.com/user" data-widget-id="id">Tweets by @user</a>
</div>
<script>CheckOnline()</script>
0

You have tryied just put "https://platform.twitter.com/widgets.js" without "p" var?

Marcelo Aymone
  • 293
  • 3
  • 19