HTML5离线应用

发布于 2024-10-18 12:01:52 字数 167 浏览 6 评论 0原文

我正在尝试在单个 html 页面中应用 html5 离线应用程序。 Google、Jquery 或其他服务提供商是否有任何单行链接 API(例如我们使用 Google 地图链接)可以轻松启用离线缓存? 我是 html5 和 Javascript 新手,请帮助我。

多谢。

I am trying to apply html5 offline application in a single html page.
Is there any one line link API(like we use Google map link) by Google, Jquery or some other service provider which can easily enable offline cache?
I am new to html5 and Javascript please help me out.

Thanks a lot.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

厌味 2024-10-25 12:01:52

这并不像单链接包含那么容易。至少,您需要一个清单文件。该文件告诉您的应用程序应缓存哪些文件。您可以在此处了解更多信息。以下是清单文件内容的示例:

清单内容

CACHE MANIFEST
/clock.css
/clock.js
/clock-face.jpg

您还可以阅读这个关于清单/离线应用程序的非常好的教程,这里

准备好清单文件后,您可以使用以下代码来测试您的清单以及缓存和未缓存的内容、已缓存的内容等。

调试代码:

var cacheStatusValues = [];
cacheStatusValues[0] = 'uncached';
cacheStatusValues[1] = 'idle';
cacheStatusValues[2] = 'checking';
cacheStatusValues[3] = 'downloading';
cacheStatusValues[4] = 'updateready';
cacheStatusValues[5] = 'obsolete';

// Listeners for all possible events
var cache = window.applicationCache;
cache.addEventListener('cached', logEvent, false);
cache.addEventListener('checking', logEvent, false);
cache.addEventListener('downloading', logEvent, false);
cache.addEventListener('error', logEvent, false);
cache.addEventListener('noupdate', logEvent, false);
cache.addEventListener('obsolete', logEvent, false);
cache.addEventListener('progress', logEvent, false);
cache.addEventListener('updateready', logEvent, false);

// Log every event to the console
function logEvent(e) {
    var online, status, type, message;
    online = (navigator.onLine) ? 'yes' : 'no';
    status = cacheStatusValues[cache.status];
    type = e.type;
    message = 'online: ' + online;
    message+= ', event: ' + type;
    message+= ', status: ' + status;
    if (type == 'error' && navigator.onLine) {
        message+= ' (prolly a syntax error in manifest)';
    }
    console.log(message);
}

// Swap in newly downloaded files when update is ready
window.applicationCache.addEventListener(
    'updateready', 
    function(){
        window.applicationCache.swapCache();
        console.log('swap cache has been called');
    }, 
    false
);

希望这会有所帮助。

It's not really as easy as a one-link inclusion. At bare minimum, you need a manifest file. This file tells your application what files should be cached. You can read more on that here. Here is a sample of a manifest files contents:

Manifest content

CACHE MANIFEST
/clock.css
/clock.js
/clock-face.jpg

You can also read this pretty good tutorial on manifest/offline applications, here.

Once you have a manifest file in place, you can use the following code to test your manifest and what is and isn't caching, cached, etc..

Debug code:

var cacheStatusValues = [];
cacheStatusValues[0] = 'uncached';
cacheStatusValues[1] = 'idle';
cacheStatusValues[2] = 'checking';
cacheStatusValues[3] = 'downloading';
cacheStatusValues[4] = 'updateready';
cacheStatusValues[5] = 'obsolete';

// Listeners for all possible events
var cache = window.applicationCache;
cache.addEventListener('cached', logEvent, false);
cache.addEventListener('checking', logEvent, false);
cache.addEventListener('downloading', logEvent, false);
cache.addEventListener('error', logEvent, false);
cache.addEventListener('noupdate', logEvent, false);
cache.addEventListener('obsolete', logEvent, false);
cache.addEventListener('progress', logEvent, false);
cache.addEventListener('updateready', logEvent, false);

// Log every event to the console
function logEvent(e) {
    var online, status, type, message;
    online = (navigator.onLine) ? 'yes' : 'no';
    status = cacheStatusValues[cache.status];
    type = e.type;
    message = 'online: ' + online;
    message+= ', event: ' + type;
    message+= ', status: ' + status;
    if (type == 'error' && navigator.onLine) {
        message+= ' (prolly a syntax error in manifest)';
    }
    console.log(message);
}

// Swap in newly downloaded files when update is ready
window.applicationCache.addEventListener(
    'updateready', 
    function(){
        window.applicationCache.swapCache();
        console.log('swap cache has been called');
    }, 
    false
);

Hope this helps.

狼性发作 2024-10-25 12:01:52

一般来说,html5 离线功能是通过创建一个清单文件来启用的,该清单文件描述了浏览器应缓存应用程序中的哪些资源。

阅读此

http://www.webreference.com/authoring/languages/ html/HTML5-Application-Caching/

以及一般谷歌搜索“html5 清单”。

我不知道单页应用程序会受益多少;您的应用程序需要能够离线运行才能获得好处。例如,电子邮件 RIA 可能允许您离线撰写消息,并将其存储在本地存储中,直到您在线并可以发送消息。同样,它可能会在在线时加载并存储您的电子邮件,以便您可以在离线时查看您的消息......

in general the html5 offline capabilities are enabled by creating a manifest file that describes what resource in your app should be cached by the browser.

Read this

http://www.webreference.com/authoring/languages/html/HTML5-Application-Caching/

and also general google search for 'html5 manifest'.

I don't know how much benefit a one page application will benefit; your app needs to be able to function offline to gain a benefit. So for example, an email RIA might allow you to compose a message offline, and would store it in local storage until you are online and could send the message. similarly, it might load and store your emails when online so you could review your messages while offline...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文