This is an experimental technology
Check the Browser compatibility table carefully before using this in production.
The set() method of the 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.
URLSearchParams.set(name, value)
Void
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);
This is a real-life example demonstrating how to create a URL
and set some search parameters.
You can copy&paste the example in your [Scratchpad]
'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' )
Specification | Status | Comment |
---|---|---|
URL The definition of 'set()' in that specification. | Living Standard | Initial definition. |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 49 | 17 | 26 | No | 36 | ? |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | 49 | 49 | 17 | 26 | 36 | ? | ? |
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams/set