I’m trying to do something with javascript (I’m a beginner and I’m studying it) and I’d like to know how can I open a link saved in a variable. I’m trying with …
<input type="button" onclick="document.location.href=Query;" />
Where Query is a variable in the method Ricerca that works with another button
function ricerca()
{
var Link = "http://www.mysite.com/search?q=variabile&k=&e=1";
var Name= document.getElementById('utente').value;
var Query = Link.replace("variabile",Name);
alert(Query);
return false;
}
the other button generates a custom search link …
input type="text" id="utente">
<input type="submit" value="Click me" onclick="return ricerca();" />
What’s wrong with my code?
This markup:
…would require that you have a global variable called
Query, which you don’t have. You have a local variable within a function. You’ll need to have a function (possibly yourricercafunction?) return the URL, and then call the function. Something like this:and
Separately, just use
location.hrefrather thandocument.location.href.locationis a global variable (it’s a property ofwindow, and allwindowproperties are globals), and that’s the one you use to load a new page.