I am working with HTML and Javascript. I am trying to extract the first URL parameter and put it in variable1
variable in my script tag.
Below is my code
<html>
<head>
<title>Applying</title>
</head>
<body>
<script type="text/javascript"
urlId="420"
dataTitle= variable1;
dataemail="admin@domain.net">
</script>
</body>
</html>
And I am not sure how to extract the first parameter from the URL and put it in my variable1
variable in my script tag.
Suppose if the url is like this-
test.html?parameter1=hello
then variable1
variable in my script tag should have hello
value after extraction. Any idea how this can be done? Any help will be appreciated.
Updated Code That I have tried
<html>
<head>
<title>Applying</title>
</head>
<body>
<script>
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var variable1 = getUrlVars()["parameter1"];
</script>
<script type="text/javascript"
urlId="420"
dataTitle= variable1;
dataemail="admin@domain.net">
</script>
</body>
</html>
Will the above code do the required thing that I am looking for?