URL笔记

完整的URL由这几个部分组成

scheme://host:port/path?query#fragment 
scheme(通信协议):常用的http,ftp,maito等

host(主机): 服务器(计算机)域名系统 (DNS) 主机名或 IP 地址。

port(端口号):整数,可选,省略时使用方案的默认端口,如http的默认端口为80。

path(路径):由零或多个"/"符号隔开的字符串,一般用来表示主机上的一个目录或文件地址。

query(查询):可选,用于给动态网页(如使用CGI、ISAPI、PHP/JSP/ASP/ASP.NET等技术制作的网页)传递参数,可有多个参数,用"&"符号隔开,每个参数的名和值用"="符号隔开。

fragment(信息片断):字符串,用于指定网络资源中的片断。例如一个网页中有多个名词解释,可使用fragment直接定位到某一名词解释。(也称为锚点.)

实例:http://www.gq1986.com:8080/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere

var url = window.location.href
console.log("完整的 URL ", url)
// http://www.gq1986.com:8080/fisker/post/0703/window.location.html?ver=1.0&id=6#imhere

var urlPr = window.location.protocol
console.log("URL 的协议", urlPr)
// http:

var urlHo = window.location.host
console.log("URL 的域名", urlHo)
// www.gq1986.com

var urlPo = window.location.port
console.log("URL 的端口", urlPo)
// 8080

var urlPa = window.location.pathname
console.log("URL 的路径", urlPa)
// /fisker/post/0703/window.location.html

var urlSe = window.location.search
console.log("查询 URL(参数)部分", urlSe)
// ?ver=1.0&id=6

var urlHa = window.location.hash
console.log("可以用来获取或设置页面的标签值,锚点", urlHa)
//#imhere

猜你喜欢

转载自blog.csdn.net/gqzydh/article/details/81086128