How to Write and Update HTML File with Jquery?

Asked

Viewed 281 times

0

So I’m taking the 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 the APP will only work if user is connected to the internet. What I wanted was to save this content in a file and upload the information from that file, which would be updated every time the user opened the application connected to the internet. Is there a function that writes the content I picked up in a . html file with Jquery?

  • You can use the client browser’s localStorage to store the request data and use it as "offline cache" - https://medium.com/dev-channel/offline-storage-for-progressiveweb-apps-70d52695513c

1 answer

1


Well, you can check the answer at can create txt with js?

If it’s an app and you’re using jquery, you’re probably using Cordova for development(confirm me) then you can also use the plugin File of Cordova:

https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file/index.html

However, as well commented by @Romulo Gabriel you can use localstorage and then use a function to check whether or not 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();
  • That’s it, I’m using the Color yes. Perfect! I was so surprised that Jquery did everything I imagined that I didn’t even think of other ways to do it. Thank you! Lucas Torres and @Romulogabriel

Browser other questions tagged

You are not signed in. Login or sign up in order to post.