Created
November 20, 2018 13:25
-
-
Save chhib/4ba7238d595828fd6b7dca73da8c72ac to your computer and use it in GitHub Desktop.
Code from the Query String Parameters video on DevTips
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const URL = 'https://www.adlibris.com' + | |
'/se/bok/factfulness-tio-knep-som-hjalper-dig-forsta-varlden-9789127149946' + | |
'?filter=hardcover' + | |
'&gclid=760a19f0-5057-417a-8e6a-b44a01b12a46' + | |
'&some=thing' | |
const [first, last] = URL.split("?") | |
const URLwithoutGCLID = first | |
URLwithoutGCLID |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const URL = 'https://www.adlibris.com' + | |
'/se/bok/factfulness-tio-knep-som-hjalper-dig-forsta-varlden-9789127149946' + | |
'?hej=monika' + | |
'&filter=hardcover' + | |
'&some=thing' + | |
'&gclid=760a19f0-5057-417a-8e6a-b44a01b12a46' | |
const a = window.document.createElement('a') | |
a.href = URL | |
let params = new URLSearchParams(a.search) | |
params.delete('gclid') | |
const first = URL.split("?")[0] | |
const newParams = params.toString() | |
const URLwithoutGCLID = `${first}?${newParams}` | |
URLwithoutGCLID | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const URL = 'https://www.adlibris.com' + | |
'/se/bok/factfulness-tio-knep-som-hjalper-dig-forsta-varlden-9789127149946' + | |
'?gclid=760a19f0-5057-417a-8e6a-b44a01b12a46' + | |
'&filter=hardcover' + | |
'&some=thing' | |
/* | |
Using Kip's answer at: | |
https://stackoverflow.com/questions/1842681/regular-expression-to-remove-one-parameter-from-query-string | |
*/ | |
const reFilterGCLID = /&gclid(\=[^&]*)?(?=&|$)|^gclid(\=[^&]*)?(&|$)/ | |
const [first, last] = URL.split("?") | |
const paramsWithoutGCLID = last.replace(reFilterGCLID, '') | |
const URLwithoutGCLID = `${first}?${paramsWithoutGCLID}` | |
URLwithoutGCLID | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment