Home Web Front-end H5 Tutorial html5 guide-2. How to operate document metadata_html5 tutorial tips

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

May 16, 2016 pm 03:50 PM
document

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:
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run the h5 project How to run the h5 project Apr 06, 2025 pm 12:21 PM

Running the H5 project requires the following steps: installing necessary tools such as web server, Node.js, development tools, etc. Build a development environment, create project folders, initialize projects, and write code. Start the development server and run the command using the command line. Preview the project in your browser and enter the development server URL. Publish projects, optimize code, deploy projects, and set up web server configuration.

What exactly does H5 page production mean? What exactly does H5 page production mean? Apr 06, 2025 am 07:18 AM

H5 page production refers to the creation of cross-platform compatible web pages using technologies such as HTML5, CSS3 and JavaScript. Its core lies in the browser's parsing code, rendering structure, style and interactive functions. Common technologies include animation effects, responsive design, and data interaction. To avoid errors, developers should be debugged; performance optimization and best practices include image format optimization, request reduction and code specifications, etc. to improve loading speed and code quality.

How to make h5 click icon How to make h5 click icon Apr 06, 2025 pm 12:15 PM

The steps to create an H5 click icon include: preparing a square source image in the image editing software. Add interactivity in the H5 editor and set the click event. Create a hotspot that covers the entire icon. Set the action of click events, such as jumping to the page or triggering animation. Export H5 documents as HTML, CSS, and JavaScript files. Deploy the exported files to a website or other platform.

What is the H5 programming language? What is the H5 programming language? Apr 03, 2025 am 12:16 AM

H5 is not a standalone programming language, but a collection of HTML5, CSS3 and JavaScript for building modern web applications. 1. HTML5 defines the web page structure and content, and provides new tags and APIs. 2. CSS3 controls style and layout, and introduces new features such as animation. 3. JavaScript implements dynamic interaction and enhances functions through DOM operations and asynchronous requests.

Is H5 page production a front-end development? Is H5 page production a front-end development? Apr 05, 2025 pm 11:42 PM

Yes, H5 page production is an important implementation method for front-end development, involving core technologies such as HTML, CSS and JavaScript. Developers build dynamic and powerful H5 pages by cleverly combining these technologies, such as using the &lt;canvas&gt; tag to draw graphics or using JavaScript to control interaction behavior.

What application scenarios are suitable for H5 page production What application scenarios are suitable for H5 page production Apr 05, 2025 pm 11:36 PM

H5 (HTML5) is suitable for lightweight applications, such as marketing campaign pages, product display pages and corporate promotion micro-websites. Its advantages lie in cross-platformity and rich interactivity, but its limitations lie in complex interactions and animations, local resource access and offline capabilities.

How to make pop-up windows with h5 How to make pop-up windows with h5 Apr 06, 2025 pm 12:12 PM

H5 pop-up window creation steps: 1. Determine the triggering method (click, time, exit, scroll); 2. Design content (title, text, action button); 3. Set style (size, color, font, background); 4. Implement code (HTML, CSS, JavaScript); 5. Test and deployment.

What Does H5 Refer To? Exploring the Context What Does H5 Refer To? Exploring the Context Apr 12, 2025 am 12:03 AM

H5referstoHTML5,apivotaltechnologyinwebdevelopment.1)HTML5introducesnewelementsandAPIsforrich,dynamicwebapplications.2)Itsupportsmultimediawithoutplugins,enhancinguserexperienceacrossdevices.3)SemanticelementsimprovecontentstructureandSEO.4)H5'srespo

See all articles