Home > Web Front-end > H5 Tutorial > body text

html5 guide-2. How to operate document metadata_html5 tutorial tips

WBOY
Release: 2016-05-16 15:50:15
Original
1368 people have browsed it

Today’s content is about how to operate document objects.
1. Manipulate Document Metadata
First, let’s take a look at the related attributes:
characterSet: Get the encoding method of the current document. This attribute is read-only;
charset: Get Or set the encoding method of the current document;
compatMode: Get the compatibility mode of the current document;
cookie: Get or set the cookie object of the current document;
defaultCharset: Get the browser's default encoding method;
defaultView: Get the window object of the current document;
dir: Get or set the text alignment of the current document;
domain: Get or set the domian value of the current document;
implementation: Provide the supported dom features information;
lastModified: Get the last modified time of the document (if there is no last modified time, return the current time);
location: Provide the URL information of the current document;
readyState: Return the status of the current document, This attribute is a read-only attribute;
referrer: Returns the document url information connected to the current document;
title: Gets or sets the title of the current document.
Look at the following example:

Copy the code
The code is as follows:




example



< /body>


Results (different browsers may display different results):


2. How to understand compatibility mode
The compatMode attribute tells you how the browser handles the current document. There is so much non-standard HTML out there that browsers will try to display these pages even though they don't conform to the HTML specification. Some content relies on unique features that existed during the early days of the browser wars, and these properties are not compliant with the specification. compatMode will return one or two values, as follows:
CSS1Compat: the document conforms to a valid html specification (not necessarily html5, the verified html4 page also returns this value);
BackCompat: the document contains non-compliant feature, triggering compatibility mode.
3. Use the Location object
document.location returns a Location object, providing you with fine-grained address information of the document and allowing you to navigate to other documents.
protocol: Get or set the protocol of the document url;
host: Get or set the host information of the document url;
href: Get or set the address information of the document;
hostname: Get or set the document's address information Host name;
search: Get or set the information in the query part of the document url;
hash: Get or set the information in the hash part of the document url;
assign(): Navigate to a specified url;
replace(): Remove the current document and navigate to the specified url;
reload(): Reload the current document;
resolveURL(): Change the relative path to Absolute path.
Look at the following example:

Copy the code
The code is as follows:











结果:


4.读写cookie
通过cookie属性,可以对document的cookie进行新增,修改和读取操作。如下例:

复制代码
代码如下:




Example













结果:


5.理解ReadyState
document.readyState帮助你了解页面加载和解析过程中,页面所处的当前状态。需要记住的一点是,浏览器当遇到script元素时会立即执行,除非你使用defer属性延时脚本的执行。readyState有三个值代表不同的状态。
loading:浏览器正在加载和执行document;
interactive:docuent已经完成解析,但是浏览器正在加载其他外部资源(media,图片等);
complete:页面解析完成,外部资源在家完毕。
在浏览器整个加载和解析的过程中,readyState的值会从loading,interactive和complete逐个改变。当结合readystatechange事件(readyState状态改变时触发)使用,readyState会变得相当有价值。

复制代码
代码如下:




Example


<script> <br>document.onreadystatechange = function () { <br> if (document.readyState == "interactive") { <br>document.getElementById("pressme").onclick = function () { <br>document.getElementById("results").innerHTML = "Button Pressed"; <br>} <br>} <br>} <br></script>



 



above The code uses the readystatechange event to achieve the effect of delayed execution. The value of readystate will become interactive only after the entire page is parsed and contacted. At this time, the click event is bound to the pressme button. This operation can ensure that the required html elements are present and prevent errors from occurring.
6. Get information about the implementation of dom attributes
The document.implementation attribute helps you understand the browser's implementation of dom attributes. This property returns a DOMImplementation object, which contains the hasFeature method. You can use this method to understand the browser's implementation of a certain property.

Copy code
The code is as follows:




Example




<script> <br>var features = ["Core", "HTML" , "CSS", "Selectors-API"]; <br>var levels = ["1.0", "2.0", "3.0"]; <br>document.writeln("<pre class="brush:php;toolbar:false">&quot;); &lt;br&gt; for (var i = 0; i &lt; features.length; i ) { &lt;br&gt;document.writeln(&quot;Checking for feature: &quot; features[i]); &lt;br&gt;for (var j = 0; j &lt; levels .length; j ) { &lt;br&gt;document.write(features[i] &quot; Level &quot; levels[j] &quot;: &quot;); &lt;br&gt;document.writeln(document.implementation.hasFeature(features[i], levels[j ])); &lt;br&gt;} &lt;br&gt;} &lt;br&gt;document.write(&quot;</pre>") <br></script>


Effect:
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template