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>';
}