set() 方法在 URLSearchParams interface sets the value associated with a given search parameter to the given value. If there were several matching values, this method deletes the others. If the search parameter doesn't exist, this method creates it.

注意: 此特征可用于 Web 工作者 .

句法

URLSearchParams.set(name, value)
					

参数

名称

The name of the parameter to set.

The value of the parameter to set.

返回值

Void.

范例

Let's start with a simple example:

let url = new URL('https://example.com?foo=1&bar=2');
let params = new URLSearchParams(url.search.slice(1));
//Add a third parameter.
params.set('baz', 3);
params.toString(); // "foo=1&bar=2&baz=3"
					

Below is a real-life example demonstrating how to create a URL and set some search parameters.

You can copy and paste the example in a code environment like Codepen, JSFiddle, or the multi-line JavaScript interpreter in Firefox .

  • line #41: Comment out this line to stop dumping the search parameters to the console (debug).
  • line #43: Dumps the generated object and it's string representation to the console (info).
  • line #44: Tries to automatically open a new window/tab with the generated URL (when uncommented).
'use strict'
function genURL(rExp, aText, bDebug=false){
	let theURL
	theURL= new URL('https://regexr.com')
	theURL.searchParams.set( 'expression', rExp.toString() )
	theURL.searchParams.set( 'tool', 'replace' )
	theURL.searchParams.set( 'input', '\u2911\u20dc' )// ⤑⃜
	theURL.searchParams.set( 'text', aText.join('\n') )
	if( bDebug ){
		// Display the key/value pairs
		for(var pair of theURL.searchParams.entries()) {
			console.debug(pair[0] + ' = \'' + pair[1] + '\'');
		}
		console.debug(theURL)
	}
	return theURL
}
var url = genURL(
	/(^\s*\/\/|\s*[^:]\/\/).*\s*$|\s*\/\*(.|\n)+?\*\/\s*$/gm	// single/multi-line comments
	// /(^\s*\/\/.*|\s*[^:]\/\/.*)/g								// single-line comments
	,[
		"These should work:",
		"",
		"// eslint-disable-next-line no-unused-vars",
		"lockPref(	'keyword.URL',\t\t'https://duckduckgo.com/html/?q=!+'	)\t//      test",
		"/*",
		"	* bla bla    ",
		"*/",
		"",
		"/* bla bla */",
		"",
		"// bla bla ",
		"",
		"These shouldn\'t work:",
		"console.log(\"http://foo.co.uk/\")",
		"var url = \"http://regexr.com/foo.html?q=bar\"",
		"alert(\"https://mediatemple.net\")",
	]
	, true
)
console.info( url, url.toString() )
// window.open( url, 'regex_site' )
					

规范

规范 状态 注释
URL
The definition of 'set()' in that specification.
实时标准 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
set Chrome 49 Edge 17 Firefox 29 IE 不支持 No Opera 36 Safari Yes WebView Android 49 Chrome Android 49 Firefox Android 29 Opera Android 36 Safari iOS Yes Samsung Internet Android 5.0

图例

完整支持

完整支持

不支持

不支持

元数据

  • 最后修改: