js 获取当前网址url参数等信息方法
有时会用到js 获取当前网址url、域名、端口号、主机、整个url字符串、路径等信息,今天特意整理一下,也可以让我复习下js的知识,,今后没事时偶偶看看,js 获取当前网址url参数等信息方法,希望对看文章的你,有用吧!
1.先看js代码:
console.log("主机部分:"+window.location.host);
console.log("域名:"+window.location.hostname);
console.log("完整url:"+window.location.href);
console.log("路径信息:"+window.location.pathname);
console.log("url 的协议:"+window.location.protocol);
console.log("url 的端口好:"+window.location.port);
2.看输出结果:
3.代码解析:
1)window.location.host;
返回url 的主机部分就是域名加端口号,例如:www.xxx.com:63342
2)window.location.hostname;
返回域名,例如:www.xxx.com
3)window.location.href;
返回整个url字符串(在浏览器中就是完整的地址栏),例如:www.xxx.com/index.php?class_id=3&id=2
4)window.location.pathname;
返回路径信息,例如:/a/index.php或者/index.php
5)window.location.protocol;
返回url 的协议部分,例如: http:,ftp:,maito:等等。
6)window.location.port
url 的端口部分,如果采用默认的80端口,那么返回值并不是默认的80而是空字符
原文地址:https://tangjiusheng.com/js/107.html
- js prototype和__proto__都有什么作用(彻底搞懂prototype、__proto__与constructor)
- js 判断数组中是否有某个值(6种方式判断包含某个特定的值)
- js for 循环(js for循环语句例题及解析)
- js try catch用法(前端try catch语句详解)
- js includes函数用法(数组中使用includes方法详解)
- js tofixed方法(js格式化数字方法)
- js URL编码(前端URL编码解码介绍)
- js localstorage用法(localstorage是什么)
- js bind函数用法(bind函数实现原理)
- js 获取当前网址url参数等信息方法