0

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?

arsenal
  • 23,366
  • 85
  • 225
  • 331
  • 1
    http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values – DarkBee Jul 10 '13 at 07:56
  • Check here https://gist.github.com/elclanrs/5696965 – elclanrs Jul 10 '13 at 07:56
  • I have already seen that example but somehow, I am not able to understand how to replace the variable1 inside script tag. And that is confusing me a lot. Any simple example will make me understand better. Thanks – arsenal Jul 10 '13 at 07:58
  • I have answered it at http://stackoverflow.com/questions/17493294/how-to-get-query-string-from-javascript-object-without-jquery/17501249#17501249 You will have to write some more javascript code. – captainsac Jul 10 '13 at 07:58
  • 1
    I have updated my question with the solution I have. Can you please take a look and let me know whether it will work or not? – arsenal Jul 10 '13 at 08:02

1 Answers1

1

Create the script element dynamically:

var myScript = document.createElement('script');

myScript.setAttribute('type', 'text/javascript');
myScript.setAttribute('urlId', '420');
myScript.setAttribute('dataTitle', variable1);
myScript.setAttribute('dataemail', 'admin@domain.net');

document.body.appendChild(myScript);

The whole solution:

<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"];    

var myScript = document.createElement('script');

myScript.setAttribute('type', 'text/javascript');
myScript.setAttribute('urlId', '420');
myScript.setAttribute('dataTitle', variable1);
myScript.setAttribute('dataemail', 'admin@domain.net');

document.body.appendChild(myScript);                              
</script>

</body>
</html>
Macros
  • 415
  • 2
  • 8