// 获取当前页面URL let href = window.location.href; console.log(href); // http://www.domain.com/test.html?name=%E5%BC%A0%E4%B8%89&age=18 // 创建URL对象和URLSearchParams对象,通过这两个对象解析当前页面URL let url = new URL(href); let params = new URLSearchParams(url.search); // 遍历URL里的所有参数 let entries = params.entries(); for (let param of entries) { console.log(param[0] + ' => ' + param[1]); } // name => 张三 // age => 18 // 获取指定参数 let name = params.get('name'); let age = params.get('age'); console.log('俺叫' + name + ',俺今年' + age + '岁。'); // 俺叫张三,俺今年18岁。 // 获取不存在的参数 let gender = params.get('gender'); console.log(gender) // null
// 假设有页面引入了JavaScript文件,代码如下: // <script type="text/javascript" src="./demo.js?name=张三&age=18"></script> // JavaScript文件获取自身URL(含参数) let src = document.scripts[document.scripts.length - 1].src; console.log(src); // http://www.domain.com/demo.js?name=%E5%BC%A0%E4%B8%89&age=18 // 创建URL对象和URLSearchParams对象,通过这两个对象解析JavaScript文件URL let url = new URL(src); let params = new URLSearchParams(url.search); // 遍历URL里的所有参数 let entries = params.entries(); for (let param of entries) { console.log(param[0] + ' => ' + param[1]); } // name => 张三 // age => 18 // 获取指定参数 let name = params.get('name'); let age = params.get('age'); console.log('俺叫' + name + ',俺今年' + age + '岁。'); // 俺叫张三,俺今年18岁。
function parse_url(str,component){var query;var mode=(typeof require!=="undefined"?require("../info/ini_get")("locutus.parse_url.mode"):undefined)||"php";var key=["source","scheme","authority","userInfo","user","pass","host","port","relative","path","directory","file","query","fragment"];var parser={php:new RegExp(["(?:([^:\\/?#]+):)?","(?:\\/\\/()(?:(?:()(?:([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?","()","(?:(()(?:(?:[^?#\\/]*\\/)*)()(?:[^?#]*))(?:\\?([^#]*))?(?:#(.*))?)"].join("")),strict:new RegExp(["(?:([^:\\/?#]+):)?","(?:\\/\\/((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?))?","((((?:[^?#\\/]*\\/)*)([^?#]*))(?:\\?([^#]*))?(?:#(.*))?)"].join("")),loose:new RegExp(["(?:(?![^:@]+:[^:@\\/]*@)([^:\\/?#.]+):)?","(?:\\/\\/\\/?)?","((?:(([^:@\\/]*):?([^:@\\/]*))?@)?([^:\\/?#]*)(?::(\\d*))?)","(((\\/(?:[^?#](?![^?#\\/]*\\.[^?#\\/.]+(?:[?#]|$)))*\\/?)?([^?#\\/]*))","(?:\\?([^#]*))?(?:#(.*))?)"].join(""))};var m=parser[mode].exec(str);var uri={};var i=14;while(i--)if(m[i])uri[key[i]]=m[i];if(component)return uri[component.replace("PHP_URL_","").toLowerCase()];if(mode!=="php"){var name=(typeof require!=="undefined"?require("../info/ini_get")("locutus.parse_url.queryKey"):undefined)||"queryKey";parser=/(?:^|&)([^&=]*)=?([^&]*)/g;uri[name]={};query=uri[key[12]]||"";query.replace(parser,function($0,$1,$2){if($1)uri[name][$1]=$2})}delete uri.source;return uri} function parse_str(str,array){var strArr=String(str).replace(/^&/,"").replace(/&$/,"").split("\x26");var sal=strArr.length;var i;var j;var ct;var p;var lastObj;var obj;var undef;var chr;var tmp;var key;var value;var postLeftBracketPos;var keys;var keysLen;var _fixStr=function(str){return decodeURIComponent(str.replace(/\+/g,"%20"))};var $global=typeof window!=="undefined"?window:global;$global.$locutus=$global.$locutus||{};var $locutus=$global.$locutus;$locutus.php=$locutus.php||{};if(!array)array=$global;for(i=0;i<sal;i++){tmp=strArr[i].split("\x3d");key=_fixStr(tmp[0]);value=tmp.length<2?"":_fixStr(tmp[1]);while(key.charAt(0)===" ")key=key.slice(1);if(key.indexOf("\x00")>-1)key=key.slice(0,key.indexOf("\x00"));if(key&&key.charAt(0)!=="["){keys=[];postLeftBracketPos=0;for(j=0;j<key.length;j++)if(key.charAt(j)==="["&&!postLeftBracketPos)postLeftBracketPos=j+1;else if(key.charAt(j)==="]")if(postLeftBracketPos){if(!keys.length)keys.push(key.slice(0,postLeftBracketPos-1));keys.push(key.substr(postLeftBracketPos,j-postLeftBracketPos));postLeftBracketPos=0;if(key.charAt(j+1)!=="[")break}if(!keys.length)keys=[key];for(j=0;j<keys[0].length;j++){chr=keys[0].charAt(j);if(chr===" "||chr==="."||chr==="[")keys[0]=keys[0].substr(0,j)+"_"+keys[0].substr(j+1);if(chr==="[")break}obj=array;for(j=0,keysLen=keys.length;j<keysLen;j++){key=keys[j].replace(/^['"]/,"").replace(/['"]$/,"");lastObj=obj;if(key!==""&&key!==" "||j===0){if(obj[key]===undef)obj[key]={};obj=obj[key]}else{ct=-1;for(p in obj)if(obj.hasOwnProperty(p))if(+p>ct&&p.match(/^\d+$/g))ct=+p;key=ct+1}}lastObj[key]=value}}} function basename(path,suffix){let b=path;const lastChar=b.charAt(b.length-1);if(lastChar==='/'||lastChar==='\\'){b=b.slice(0,-1)}b=b.replace(/^.*[/\\]/g,'');if(typeof suffix==='string'&&b.substr(b.length-suffix.length)===suffix){b=b.substr(0,b.length-suffix.length)}return b} function pathinfo(path,options){let opt='';let realOpt='';let optName='';let optTemp=0;const tmpArr={};let cnt=0;let i=0;let haveBasename=false;let haveExtension=false;let haveFilename=false;if(!path){return false}if(!options){options='PATHINFO_ALL'}const OPTS={PATHINFO_DIRNAME:1,PATHINFO_BASENAME:2,PATHINFO_EXTENSION:4,PATHINFO_FILENAME:8,PATHINFO_ALL:0};for(optName in OPTS){if(OPTS.hasOwnProperty(optName)){OPTS.PATHINFO_ALL=OPTS.PATHINFO_ALL|OPTS[optName]}}if(typeof options!=='number'){options=[].concat(options);for(i=0;i<options.length;i++){if(OPTS[options[i]]){optTemp=optTemp|OPTS[options[i]]}}options=optTemp}const _getExt=function(path){const str=path+'';const dotP=str.lastIndexOf('.')+1;return!dotP?false:dotP!==str.length?str.substr(dotP):''};if(options&OPTS.PATHINFO_DIRNAME){const dirName=path.replace(/\\/g,'/').replace(/\/[^/]*\/?$/,'');tmpArr.dirname=dirName===path?'.':dirName}if(options&OPTS.PATHINFO_BASENAME){if(haveBasename===false){haveBasename=basename(path)}tmpArr.basename=haveBasename}if(options&OPTS.PATHINFO_EXTENSION){if(haveBasename===false){haveBasename=basename(path)}if(haveExtension===false){haveExtension=_getExt(haveBasename)}if(haveExtension!==false){tmpArr.extension=haveExtension}}if(options&OPTS.PATHINFO_FILENAME){if(haveBasename===false){haveBasename=basename(path)}if(haveExtension===false){haveExtension=_getExt(haveBasename)}if(haveFilename===false){haveFilename=haveBasename.slice(0,haveBasename.length-(haveExtension?haveExtension.length+1:haveExtension===false?0:1))}tmpArr.filename=haveFilename}cnt=0;for(opt in tmpArr){if(tmpArr.hasOwnProperty(opt)){cnt++;realOpt=opt}}if(cnt===1){return tmpArr[realOpt]}return tmpArr} let url = 'https://www.domain.com/test.html#profile?name=张三&age=18'; //========== 解析URL ==========// let parse = parse_url(url); console.log(parse); // {fragment: 'profile?name=张三&age=18', path: '/test.html', host: 'www.domain.com', scheme: 'https'} //========== 解析路径 ==========// let info = pathinfo(parse.path); console.log(info); // {dirname: '', basename: 'test.html', extension: 'html', filename: 'test'} //========== 获取#号后面的内容 ==========// console.log(parse.fragment); // profile?name=张三&age=18 //========== 解析#号后面的内容 ==========// parse = parse_url(parse.fragment); console.log(parse.path); // profile console.log(parse.query); // name=张三&age=18 //========== 提取参数 ==========// let params = []; parse_str(parse.query, params) console.log(params.name); // 张三 console.log(params.age); // 18
Copyright © 2024 码农人生. All Rights Reserved