如何自动进入网址而不点击它

时间:2014-06-17 20:24:02

标签: javascript html url

我在互联网上找到了以下脚本。

如何更改代码,以便在点击Open按钮时,它会转到lnk.href"https://www.google.si/maps/place/ljubljana" + userInput;)中的地址

<body>
<script type="text/javascript">
function changeText2(){
    var userInput = document.getElementById('userInput').value;
    var lnk = document.getElementById('lnk');
    lnk.href = "https://www.google.si/maps/place/ljubljana" + userInput;
    lnk.innerHTML = lnk.href; 
}
</script>
Type the location and click Open! <a href="" id=lnk>link</a> <br>
<input type='text' id='userInput' value='' />
<input type='submit' onclick='changeText2()' value='Open'/>

1 个答案:

答案 0 :(得分:0)

您希望在功能结束时添加window.location = lnk.href;

<script>
function changeText2() {
    var userInput = document.getElementById('userInput').value;
    var lnk = document.getElementById('lnk');
    lnk.href = "https://www.google.si/maps/place/ljubljana" + userInput;
    lnk.innerHTML = lnk.href;
    window.location = lnk.href;
}
</script>

Type the location and click Open! <a href="" id=lnk>link</a> <br>
<input type='text' id='userInput' value='' />
<input type='submit' onclick='changeText2()' value='Open'/>

您也可以取消链接:

<script>
function openUrl() {
    var userInput = document.getElementById('userInput').value;
    window.location = "https://www.google.si/maps/place/ljubljana" + userInput;
}
</script>

Type the location and click Open!<br>
<input type='text' id='userInput' value='' />
<input type='submit' onclick='openUrl()' value='Open' />