Load page with HTML from web storage

I'm testing a concept for an application using local storage and need to know the best way of loading the content after storing it.

Essentially the app will prefetch content, store it locally and then server it in a new page upon request. I need users to be able to click a link (mysite.com/article1.html) and instead of having the browser make the HTTP request for the page, just load the HTML that has been stored locally.

So how do I load the "localNews" value instead of making an HTTP for the same page?

var storeUrl;
var localNews;

$('a').click(function() {
event.preventDefault();
storeUrl = $(this).attr('href');
$.ajax({
    url: storeUrl,
    cache: true,
    crossDomain: true
}).done(function(html) {
    localNews = html;
    console.log(localNews);
    localStorage.setItem('storeUrl', 'localNews');
});
});

Answers


    var storeUrl;
    var localNews;

    $('a').click(function(event) {
        event.preventDefault();
        storeUrl = $(this).attr('href');
        if (localStorage.getItem(storeUrl)) {
            //load local
        } else {
            $.ajax({
                url: storeUrl,
                cache: true,
                crossDomain: true
            }).done(function(html) {
                localNews = html;
                console.log(localNews);
                localStorage.setItem(storeUrl, localNews);
            });
        }
    });

So basically you want to load a local file via Ajax? Can be done, but just have to do a few small adjustments to your Ajax call. So change this:

$.ajax({
    url: storeUrl,
    cache: true,
    crossDomain: true

To this:

$.ajax({
    url: storeUrl,
    cache: true,
    crossDomain: true,
    async: false,
    dataType: 'html',
    contentType: 'text/html;charset=utf-8'

Note the async and datatype and contentType. It has been a while since I coded this, but I believe the key parameter is async but the datatype and contentType are needed as well.


Need Your Help

The getdata method in class file does not showing the output

asp.net

Here I declared getdata method() in class file account.vb. After clicking on GET button as a output it show the msgbox as 0 0.... so please help me:

How to cancel a sequence of UITouch events?

iphone ios uitouch touchesmoved uiresponder

I have a UIImage view that responds to touch events. I want to cancel the touch sequence, i.e., further calls to touchesMoved:, if the touch goes outside of certain bounds. How can I do that?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.