How to get a variable from a url by javascript?

Member

by adan , in category: Third Party Scripts , 4 months ago

How to get a variable from a url by javascript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 4 months ago

@adan 

You can use the URLSearchParams interface to get a variable from a URL in JavaScript. Here's an example:

  1. Get the URL of the current page:
1
const urlParams = new URLSearchParams(window.location.search);


  1. Get the value of a specific variable from the URL:
1
const variableValue = urlParams.get('variableName');


Replace 'variableName' with the name of the variable you want to retrieve from the URL. This code will parse the URL of the current page and extract the value of the specified variable.

Related Threads:

How to get # url value with javascript?
How to get the redirected url using javascript?
How to clear a variable value in JavaScript?
How to pass javascript variable to graphql query?
How to pass a javascript variable to a PHP file?
How to use javascript variable in graphql query variables?