JavaScript獲取當前頁面url參數(shù)的方法詳解
一、使用 URLSearchParams(現(xiàn)代瀏覽器支持)
URLSearchParams 是 JavaScript 提供的一個內(nèi)置對象,用于處理 URL 的查詢字符串,它提供了一系列方便的方法來獲取、設(shè)置和刪除查詢參數(shù)。
// 獲取當前頁面的 URL 參數(shù) const queryString = window.location.search; // 創(chuàng)建 URLSearchParams 對象 const urlParams = new URLSearchParams(queryString); // 獲取特定參數(shù)的值 const paramValue = urlParams.get("paramName"); console.log(paramValue); // 遍歷所有參數(shù) urlParams.forEach((value, key) => { console.log(`${key}: ${value}`); });
二、手動解析查詢字符串
手動解析查詢字符串是一種兼容性較好的方法,通過字符串的分割和處理來獲取參數(shù)。
function getUrlParams() { const queryString = window.location.search.slice(1); // 去除問號 const params = {}; if (queryString) { const paramPairs = queryString.split("&"); paramPairs.forEach((pair) => { const [key, value] = pair.split("="); if (key) { params[key] = decodeURIComponent(value || ""); } }); } return params; } // 使用示例 const allParams = getUrlParams(); const specificParam = allParams["paramName"]; console.log(specificParam);
三、在單頁面應用(SPA)中使用路由庫獲取參數(shù)
在基于 Vue.js、React.js 等框架構(gòu)建的單頁面應用中,通常會使用路由庫來管理頁面導航,這些路由庫也提供了方便的方法來獲取 URL 參數(shù)。
1. Vue.js 示例(使用 Vue Router)
<template> <div> <p>參數(shù)值: {{ $route.query.paramName }}</p> </div> </template> <script> export default { mounted() { const paramValue = this.$route.query.paramName; console.log(paramValue); }, }; </script>
2. React.js 示例(使用 React Router)
import { useSearchParams } from "react-router-dom"; function MyComponent() { const [searchParams] = useSearchParams(); const paramValue = searchParams.get("paramName"); return ( <div> <p>參數(shù)值: {paramValue}</p> </div> ); } export default MyComponent;
四、方法擴展
下面小編為大家整理了JavaScript獲取當前頁面url的各類參數(shù)的方法,希望對大家有所幫助
Location 對象
Location 對象包含有關(guān)當前 URL 的信息
Location 對象是 Window 對象的一個部分,可通過 window.location 屬性來訪問
Location的各種屬性:
- window.location.hash => 設(shè)置或返回從井號 (#) 開始的 URL(錨)
- window.location.host => 設(shè)置或返回主機名和當前 URL 的端口號
- window.location.hostname => 設(shè)置或返回當前 URL 的主機名
- window.location.href => 設(shè)置或返回完整的 URL
- window.location.pathname => 設(shè)置或返回當前 URL 的路徑部分
- window.location.port => 設(shè)置或返回當前 URL 的端口號
- window.location.protocal => 設(shè)置或返回當前 URL 的協(xié)議
- window.location.search => 設(shè)置或返回從問號 (?) 開始的 URL(查詢部分)
方法一:
// 根據(jù)參數(shù)名獲取url中的參數(shù) function getQueryString(name) { const reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); const urlObj = window.location; var r = urlObj.href.indexOf('#') > -1 ? urlObj.hash.split("?")[1].match(reg) : urlObj.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } alert(getQueryString("參數(shù)名稱"));
方法二:
// 根據(jù)參數(shù)名獲取url中的參數(shù) function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); //獲取url中"?"符后的字符串并正則匹配 var context = ""; if (r != null) context = r[2]; reg = null; r = null; return context == null || context == "" || context == "undefined" ? "" : context; } // 調(diào)用 var 參數(shù)1 = GetQueryString['參數(shù)1']; var 參數(shù)2 = GetQueryString['參數(shù)2']; var 參數(shù)3 = GetQueryString['參數(shù)3'];
方法三:
// 根據(jù)參數(shù)名獲取url中的參數(shù) function GetRequest() { var url = location.search; //獲取url中"?"符后的字串 var theRequest = new Object(); if (url.indexOf("?") != -1) { var str = url.substr(1); strs = str.split("&"); for(var i = 0; i < strs.length; i ++) { theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]); } } return theRequest; } // 調(diào)用 var Request = new Object(); Request = GetRequest(); var 參數(shù)1,參數(shù)2,參數(shù)3,參數(shù)N; 參數(shù)1 = Request['參數(shù)1']; 參數(shù)2 = Request['參數(shù)2']; 參數(shù)3 = Request['參數(shù)3'];
到此這篇關(guān)于JavaScript獲取當前頁面url參數(shù)的方法詳解的文章就介紹到這了,更多相關(guān)JavaScript頁面url參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用JavaScript解析URL參數(shù)為對象的多種方法
- JavaScript 獲取 URL 中參數(shù)值的方法
- JavaScript 獲取 URL 中參數(shù)值的方法詳解(最新整理)
- JavaScript獲取URL中參數(shù)值的四種方法
- JavaScript實現(xiàn)獲取URL中參數(shù)值的4種方法
- 使用JavaScript獲取URL參數(shù)的方法總結(jié)
- JavaScript獲取URL參數(shù)的幾種方法小結(jié)
- JavaScript獲取URL參數(shù)常見的4種方法
- JavaScript獲取URL參數(shù)的四種方法總結(jié)
- js獲取url參數(shù)值的幾種方式詳解
- JS獲取URL網(wǎng)址中參數(shù)的幾種方法小結(jié)
相關(guān)文章
基于JavaScript Array數(shù)組方法(新手必看篇)
下面小編就為大家?guī)硪黄贘avaScript Array數(shù)組方法(新手必看篇)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08IE6/7 and IE8/9/10(IE7模式)依次隱藏具有absolute或relative的父元素和子元素后再顯示
多數(shù)情況下隱藏(設(shè)置display:none)一個元素,無需依次將其內(nèi)的所有子元素都隱藏。非要這么做,有時會碰到意想不到的bug。2011-07-07JS實現(xiàn)將Asp.Net的DateTime Json類型轉(zhuǎn)換為標準時間的方法
這篇文章主要介紹了JS實現(xiàn)將Asp.Net的DateTime Json類型轉(zhuǎn)換為標準時間的方法,涉及javascript針對時間與日期操作的相關(guān)技巧,需要的朋友可以參考下2016-08-08js window.print實現(xiàn)打印特定控件或內(nèi)容
window.print可以打印網(wǎng)頁,但有時候我們只希望打印特定控件或內(nèi)容,怎么辦呢?可以把要打印的內(nèi)容放在div中,然后用下面的代碼進行打印,希望對大家有所幫助2013-09-09