html5 guide-2. How to operate document metadata_html5 tutorial tips
May 16, 2016 pm 03:50 PMToday’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:
<!DOCTYPE html>
<html>
<head>
<title>example</title>
</head>
<body>
<script type="text/javascript">
document.writeln('<pre>');
document.writeln('characterSet:' document.characterSet);
document.writeln( 'charset:' document.charset);
document.writeln('compatMode:' document.compatMode);
document.writeln('defaultCharset:' document.defaultCharset);
document.writeln('dir :' document.dir);
document.writeln('domain:' document.domain);
document.writeln('lastModified:' document.lastModified);
document.writeln('referrer:' document.referrer);
document.writeln('title:' document.title);
document.write('</pre>');
</script>
< /body>
</html>
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(<url>): Navigate to a specified url;
replace(<url>): Remove the current document and navigate to the specified url;
reload(): Reload the current document;
resolveURL(<url>): Change the relative path to Absolute path.
Look at the following example:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
document.writeln('<pre>');
document.writeln('protocol:' document.location.protocol);
document.writeln('host:' document.location.host);
document.writeln('hostname:' document.location.hostname);
document.writeln('port:' document.location.port);
document.writeln('pathname:' document.location.pathname);
document.writeln('search:' document.location.search);
document.writeln('hash:' document.location.hash);
document.writeln('</pre>');
</script>
</body>
</html>
结果:
4.读写cookie
通过cookie属性,可以对document的cookie进行新增,修改和读取操作。如下例:
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman" />
<meta name="description" content="A simple example" />
</head>
<body>
<p id="cookiedata">
</p>
<button id="write">
Add Cookie</button>
<button id="update">
Update Cookie</button>
<button id="clear">
Clear Cookie</button>
<script type="text/javascript">
var cookieCount = 0;
document.getElementById('update').onclick = updateCookie;
document.getElementById('write').onclick = createCookie;
document.getElementById('clear').onclick = clearCookie;
readCookies();
function readCookies() {
document.getElementById('cookiedata').innerHTML = !document.cookie ? '' : document.cookie;
}
function updateCookie() {
document.cookie = 'cookie_' cookieCount '=update_' cookieCount;
readCookies();
}
function createCookie() {
cookieCount ;
document.cookie = 'cookie_' cookieCount '=value_' cookieCount;
readCookies();
}
function clearCookie() {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var arrStr = document.cookie.split("; ");
for (var i = 0; i < arrStr.length; i ) {
var temp = arrStr[i].split("=");
if (temp[0]) {
document.cookie = temp[0] "=;expires=" exp.toGMTString();
};
}
cookieCount = 0;
readCookies();
}
</script>
</body>
</html>
结果:
5.理解ReadyState
document.readyState帮助你了解页面加载和解析过程中,页面所处的当前状态。需要记住的一点是,浏览器当遇到script元素时会立即执行,除非你使用defer属性延时脚本的执行。readyState有三个值代表不同的状态。
loading:浏览器正在加载和执行document;
interactive:docuent已经完成解析,但是浏览器正在加载其他外部资源(media,图片等);
complete:页面解析完成,外部资源在家完毕。
在浏览器整个加载和解析的过程中,readyState的值会从loading,interactive和complete逐个改变。当结合readystatechange事件(readyState状态改变时触发)使用,readyState会变得相当有价值。
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content ="Adam Freeman" />
<meta name="description" content="A simple example" />
<script>
document.onreadystatechange = function () {
if (document.readyState == "interactive") {
document.getElementById("pressme").onclick = function () {
document.getElementById("results").innerHTML = "Button Pressed";
}
}
}
</script>
</head>
<body>
<button id="pressme">
Press Me</button>
<pre id="results"></pre>
</body>
</html>
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.
<!DOCTYPE HTML>
<html>
<head>
<title>Example</title>
<meta name="author" content="Adam Freeman" />
<meta name ="description" content="A simple example" />
</head>
<body>
<script>
var features = ["Core", "HTML" , "CSS", "Selectors-API"];
var levels = ["1.0", "2.0", "3.0"];
document.writeln("<pre>");
for (var i = 0; i < features.length; i ) {
document.writeln("Checking for feature: " features[i]);
for (var j = 0; j < levels .length; j ) {
document.write(features[i] " Level " levels[j] ": ");
document.writeln(document.implementation.hasFeature(features[i], levels[j ]));
}
}
document.write("</pre>")
</script>
</body>
</html> ;
Effect:


Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How do I use viewport meta tags to control page scaling on mobile devices?

How to Add Audio to My HTML5 Website?

How do I use the HTML5 Page Visibility API to detect when a page is visible?

How do I handle user location privacy and permissions with the Geolocation API?

How to Use HTML5 Forms for User Input?

How to Create Interactive Games with HTML5 and JavaScript?

How do I use the HTML5 Notifications API to display desktop notifications?

How do I use the HTML5 WebSockets API for bidirectional communication between client and server?
