-1

Hii I am Shashwat I want to know how to store parameter of a url into variable in JavaScript

My url is -- www.example.com?name=shashwat&lastname=mishra&email=example@gmail.com

So I want to store like this

var name = shashwat

var lastname = mishra

var email = example@gmail.com

Shashwat
  • 11
  • 1
  • Please tell me in details how to use simple string concatenation – Shashwat Sep 03 '20 at 04:41
  • 1
    will this help you ? .... https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – KcH Sep 03 '20 at 04:42
  • 1
    Does this answer your question? [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – FluffyKitten Sep 03 '20 at 04:57

2 Answers2

0

<html>

<body>

</body>
<script>
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const product = urlParams.get('email')
console.log(product);
</script>
</html>

Please use this code to get url variable

0

You can use javascript code to get query parameter

function getUrlParameterByName(name, url) {
    if (!url) url = window.location.href;
    name = name.replace(/[\[\]]/g, '\\$&');
    var regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)'),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, ' '));
}

var email = getUrlParameterByName('email');
user_mat
  • 191
  • 2
  • 16