admin管理员组文章数量:1316371
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 = ";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?
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./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?
Share Improve this question asked Jun 18, 2012 at 6:52 user1453638user1453638 1412 gold badges3 silver badges11 bronze badges1 Answer
Reset to default 5This markup:
<input type="button" onclick="document.location.href=Query;" />
...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 your ricerca
function?) return the URL, and then call the function. Something like this:
function ricerca()
{
var Link = "http://www.mysite./search?q=variabile&k=&e=1";
var Name= document.getElementById('utente').value;
var Query = Link.replace("variabile",Name);
return Query;
}
and
<input type="button" onclick="document.location.href=ricerca();" />
Separately, just use location.href
rather than document.location.href
. location
is a global variable (it's a property of window
, and all window
properties are globals), and that's the one you use to load a new page.
本文标签: htmlJavascript button and documentlocationStack Overflow
版权声明:本文标题:html - Javascript button and document.location - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742003215a2411435.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论