最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is there anyway to change <html> tag's attributes during run time? - Stack Overflow

programmeradmin2浏览0评论

I want to add the manifest attribute during run-time, so the I can control when the Appcache will be downloaded.

Ex: When a user is logged into the application properly,

<html>
// page content
</html>

changes into:

<html manifest="myapp.manifest">
// page content
</html>

Is there anyway I can achieve this using javascript, jquery or anything? What I want to is to control when the Appcache will be downloaded conditionally.(I have already read about having another html inside an iFrame.)

I want to add the manifest attribute during run-time, so the I can control when the Appcache will be downloaded.

Ex: When a user is logged into the application properly,

<html>
// page content
</html>

changes into:

<html manifest="myapp.manifest">
// page content
</html>

Is there anyway I can achieve this using javascript, jquery or anything? What I want to is to control when the Appcache will be downloaded conditionally.(I have already read about having another html inside an iFrame.)

Share Improve this question edited Mar 27, 2014 at 6:04 JRulz asked Mar 27, 2014 at 5:30 JRulzJRulz 5173 gold badges7 silver badges17 bronze badges 0
Add a ment  | 

6 Answers 6

Reset to default 10

According to the specification, changing the manifest attribute after the document loaded has no effect.

You can still access the html element and change the attribute value, via document.documentElement:

document.documentElement.setAttribute('manifest', 'myapp.manifest');

It just won't do anything.

You can use .attr():

Get the value of an attribute for the first element in the set of matched elements or set one or more attributes for every matched element.

$('html').attr('manifest','myapp.manifest');

Normal ways to add an attribute to an element can be used, e.g.

document.documentElement.setAttribute('manifest', 'foo.appcache');

(As @FelixKing points out in a ment, assigning to document.documentElement.manifest does not work, by the specs, since manifest is not defined in the DOM. I was first misled by Chrome’s behavior in this issue.)

However, this has no effect. HTML5 CR says: “The manifest attribute only has an effect during the early stages of document load. Changing the attribute dynamically thus has no effect (and thus, no DOM API is provided for this attribute).”

(Well, it has the effect of being there, so you could use the attribute in styling, retrieve the attribute value, etc. But nothing that would cause application cache operations.)

Try this:

document.documentElement.setAttribute('manifest', 'myapp.manifest');

From the docs:

document.documentElement

Returns the Element that is the root element of the document (for example, the element for HTML documents).

Try this by jQuery.

$('html').attr('manifest', 'myapp.manifest');

It may not be possible to effectively add the manifest attribute, but it might be possible to remove it and by that you may achieve the same result.

To disable appcache, I use this:

window.console.warn('removing appcache');
window.document.documentElement.removeAttribute('manifest');

Please be carefull, this may not always work!

发布评论

评论列表(0)

  1. 暂无评论