JeremyA1 JeremyA1 - 4 years ago 101
HTML Question

Obtain variable between tag and append to URL

I have a webpage that was developed by someone else. I want to take the variable that they are displaying on when the page loads from the following tag

<h3 id="TAGS"></h3>
and append that to a this example URL
http://someurl.com/<variable>.


I'm not a JavaScript person so any help would be appreciated.

I tried the following which did not work

var myURL = $('#TAGS').innerHTML
<input type="button" value="Download" onclick="window.location.href=\'http://someurl.com/' + myURL + '\'"/>

Answer Source

JAVASCRIPT + jQuery: Create a global variable like var downloadURL = "" and make a function in jQuery:

$( document ).ready(function() {
    var myURL = $("#TAGS").html();
    downloadURL = "http://someurl.com/" + myURL;
});

and then you make your button like this:

<input type="button" value="Download" onclick="download()"/>

having the method download do whatever you want:

function download() {
   if(downloadURL!="") {
      window.location.href=downloadURL;
   }
   else {
      //you don't have a value set 
   }
   downloadURL="";
}

JAVASCRIPT: Create a global variable like var downloadURL = "" and make a function to load on page load:

window.onload = function() {
    var myURL = document.getElementById("TAGS").innerHTML;
    downloadURL = "http://someurl.com/" + myURL;
});

and then you make your button like this:

<input type="button" value="Download" onclick="download()"/>

having the method download do whatever you want:

function download() {
   if(downloadURL!="") {
      window.location=downloadURL;
   }
   else {
      //you don't have a value set 
   }
   downloadURL="";
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download