How to get a variable from a url by javascript?

Member

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

How to get a variable from a url by javascript?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by brandy , 4 days 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.