• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >html5 > html5指南(2)-操作Document metadata的详情介绍

html5指南(2)-操作Document metadata的详情介绍

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-03

本文主要包含html5,Document ,metadata等相关知识,匿名希望在学习及工作中可以帮助到您
  今天的内容是关于如何操作document对象。

  1.操作Document Metadata

  首先我们来看看相关的属性:

characterSet:获取当前document的编码方式,该属性为只读;

charset:获取或者设置当前document的编码方式;

compatMode:获取当前document的兼容模式;

cookie:获取或者设置当前document的cookie对象;

defaultCharset:获取浏览器默认的编码方式;

defaultView:获取当前当前document的window对象;

dir:获取或者设置当前document的文本对齐方式;

domain:获取或者设置当前document的domian值;

implementation:提供所支持的dom特性的信息;

lastModified:获取document最后的修改时间(如果没有最后修改时间,则返回当前时间);

location:提供当前document的url信息;

readyState:返回当前document的状态,该属性是只读属性;

referrer: 返回连接到当前document的document url信息;

title:获取或者设置当前document的title。

  来看下面的例子:

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

结果(不同浏览器显示的结果可能不一样):

  2.如何理解兼容模式

  compatMode属性告诉你浏览器是如何处理当前document的。有太多不标准的html了,浏览器会试图显示这些页面,即使他们不符合html规范。有些内容依赖于早先浏览器大战时所存在的独特的特性,而这些属性石不符合规范的。compatMode会返回一个或两个值,如下:

CSS1Compat:document符合一个有效的html规范(不一定是html5,验证的html4页面同样返回这个值);

BackCompat:document包含不符合规范的特性,触发了兼容模式。

  3.使用Location对象

  document.location返回一个Location对象,向你提供细粒度的document的地址信息,同时允许你导航到其他document。

protocol:获取或者设置document url的协议;

host:获取或者设置document url的主机信息;

href:获取或者设置document的地址信息;

hostname:获取或者设置document的主机名;

search:获取或者设置document url查询部分的信息;

hash:获取或者设置document url hash部分的信息;

assign(<url>):导航到一个指定url;

replace(<url>):移除当前document,导航到指定的url;

reload():重新加载当前document;

resolveURL(<url>):将相对路径变为绝对路径。

  来看下面的例子:

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

  


 

您可能想查找下面的文章:

  • HTML5知识点总结
  • HTML5的本地存储
  • HTML5本地存储之IndexedDB
  • Html5实现文件异步上传功能
  • Html5新标签datalist实现输入框与后台数据库数据的动态匹配
  • 详解HTML5 window.postMessage与跨域
  • HTML5拖放API实现拖放排序的实例代码
  • 解决html5中video标签无法播放mp4问题的办法
  • HTML5新特性 多线程(Worker SharedWorker)
  • Html5新增标签有哪些

相关文章

  • 2018-12-03总结关于填充与描边注意点
  • 2018-12-03HTML5 canvas绘制五角星的方法
  • 2018-12-03HTML5 Canvas绘制时指定颜色与透明度的方法
  • 2018-12-03html5 application cache遇到的严重问题
  • 2018-12-03vue跨域的解决方法
  • 2018-12-03在HTML5在线预览PDF格式的代码
  • 2018-12-03html5怎样做出图片转圈的动画效果
  • 2018-12-03HTML5中canvas的使用总结
  • 2018-12-03html5 canvas fillRect坐标和大小的问题解决方法_html5教程技巧
  • 2017-08-06html5图片上传预览示例分享

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • 涂鸦板简单实现 Html5编写属于自己的画画板
    • Html5 postMessage?
    • HTML5 离线应用之打造零请求、无流量网站的解决方法_html5教程技巧
    • HTML5学习笔记之html5与传统html区别 _html5教程技巧
    • 分段式基于SVG文字超酷创意动画特效
    • 常用的HTML5/CSS3新特性能力检测写法的示例代码分享
    • SVG基础|SVG VIEWPORT、VIEW BOX和PRESERVEASPECTRATIO
    • 在h5里手机端页面缩放应该如何实现
    • HTML5中对class属性的解释与规定
    • 详解HTML5中ol标签的用法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有