How to write and update HTML file with Jquery?

0

So, I'm getting HTML content from an external site and rendering in my hybrid application. So far so good. I have the following code so far:

$.ajax({
   url: '{{SuaUrlAqui}}',
   headers: {'X-Requested-With': 'XMLHttpRequest'},
   type: 'GET',
   success: function(res) {
      var data = $.parseHTML(res); 
      $(data).find('div#main').each(function(){
          $('div#noticias').prepend($(this).html()); //Mudar essa função aqui.
     });
   }
 });

The problem is that this page in APP will only work if user is connected to the internet. What I wanted was to save this content in a file and load that information, which would be updated every time the user opened the application connected to the internet. Is there a function that writes the content I got in a .html file with Jquery?

    
asked by anonymous 17.04.2017 / 16:30

1 answer

1

Well, you can check the answer in how to create txt with js?

If it's an app and you're using jquery, you're probably using cordova for development (confirm me) so you can also use the File plugin:

link

However, as commented by @ Romulo Gabriel you can use localstorage and then use a function to check if there is an internet connection:

function checkConnection() {
    var networkState = navigator.connection.type;

    var states = {};
    states[Connection.UNKNOWN]  = 'Unknown connection';
    states[Connection.ETHERNET] = 'Ethernet connection';
    states[Connection.WIFI]     = 'WiFi connection';
    states[Connection.CELL_2G]  = 'Cell 2G connection';
    states[Connection.CELL_3G]  = 'Cell 3G connection';
    states[Connection.CELL_4G]  = 'Cell 4G connection';
    states[Connection.CELL]     = 'Cell generic connection';
    states[Connection.NONE]     = 'No network connection';

    alert('Connection type: ' + states[networkState]);
}

checkConnection();
    
17.04.2017 / 17:06