
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境搭建
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTful API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 查詢資料
- Node.js - MySQL 條件查詢
- Node.js - MySQL 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 連線查詢
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB 排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 資料限制
- Node.js - MongoDB 連線查詢
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用工具模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
NodeJS - urlSearchParams.get() 方法
NodeJS urlSearchParams.get() 方法用於從 URLSearchParams 類中檢索查詢字串中指定名稱的值。
可以使用URLSearchParams API 讀取和寫入 URL 的查詢部分。此類也存在於全域性物件上。
為了更好地理解此方法,讓我們來看一個真實的例子。考慮這個 URL(‘https://www.youtube.com/watch?v=towpH780PT0’),其中 'v=towpH780PT0' 被稱為查詢段。在這個查詢中,(v) 是名稱,(towpH780PT0) 是值。它們一起構成一個名稱-值對。
語法
以下是NodeJS URLSearchParams.get() 方法的語法
URLSearchParams.get(name)
引數
name: 指定要返回的引數的名稱。
返回值
此方法返回作為引數傳遞的給定name 的字串值。
如果有多個具有給定名稱的名稱-值對,此方法將返回第一對中的值。
如果沒有名稱為name 的對,則返回null。
示例
在下面的示例中,我們嘗試獲取查詢字串中名為“two”的鍵的值。
const url = require('node:url'); const MyUrl = new URL('https://tutorialspoint.tw?one=1&two=2&three=3'); console.log("URL: ", MyUrl.href); const Params = new URLSearchParams('one=1&two=2&three=3'); console.log("Query string: " + Params); console.log("Trying to get the value of key 'two'....."); console.log("The value is: " + Params.get("two"));
輸出
執行上述程式後,NodeJS get() 方法將返回鍵“two”的值。
URL: https://tutorialspoint.tw/?one=1&two=2&three=3 Query string: one=1&two=2&three=3 Trying to get the value of key 'two'..... The value is: 2
示例
如果要在查詢字串中搜索的名稱多次出現,get() 方法將返回其第一次出現的值。
在下面的程式中,我們嘗試獲取查詢字串中名稱“two”的值。
const url = require('node:url'); const MyUrl = new URL('https://tutorialspoint.tw?two=1&two=2&two=3'); console.log("URL: ", MyUrl.href); const Params = new URLSearchParams('two=1&two=2&two=3'); console.log("Query string: " + Params); console.log("Trying to get the value of the first key 'two'....."); console.log("The value is: " + Params.get("two"));
輸出
正如我們在輸出中看到的,get() 方法返回第一次出現的值。
URL: https://tutorialspoint.tw/?two=1&two=2&two=3 Query string: two=1&two=2&two=3 Trying to get the value of the first key 'two'..... The value is: 1
示例
如果要在查詢字串中搜索的名稱不存在,get() 方法將返回 null。
在下面的示例中,我們嘗試獲取查詢字串中不存在的名稱的值。
const url = require('node:url'); const MyUrl = new URL('https://tutorialspoint.tw?HTML=499&NODEJS=999&CSS=699'); console.log("URL: ", MyUrl.href); const Params = new URLSearchParams('HTML=499&NODEJS=999&CSS=699'); console.log("Query string: " + Params); console.log("Trying to get the value of the first key 'JavaScript'....."); console.log("The value is: " + Params.get("JavaScript"));
輸出
正如我們在輸出中看到的,get() 方法返回 null。
URL: https://tutorialspoint.tw/?HTML=499&NODEJS=999&CSS=699 Query string: HTML=499&NODEJS=999&CSS=699 Trying to get the value of the first key 'JavaScript'..... The value is: null
nodejs_url_module.htm
廣告