0

JavaScript に少し問題があります。私の問題は、ユーザー入力ボックス (ユーザーが URL を入力する場所) とボタン (クリックすると、ユーザーが入力ボックスに入力した URL を開く) があることです。

これが私のコードです:

<input type="text" id="userurlbox"/>
<button type="button" onClick="myFunction()">Open URL</button>

<script>
function myFunction()
{
var x = document.getElementById('userurlbox').value;
if (x == "")
{
alert('Please enter a URL');
}

else
{
window.open(x ,'_blank');
}
</script>

問題は、URL が次のように開くことです。

http://mywebsite.com/USERURL

私はそれを次のように開きたい:

http://USERURL
4

3 に答える 3

0

'http://'テストしたばかりですが、window.openに を含める必要があります。

<input type="text" id="userurlbox"/>
<button type="button" onClick="myFunction()">Open URL</button>

<script>
function myFunction(){
var x = document.getElementById('userurlbox').value;

    x = x.replace('http://'.''); // remove  http:// just in-case it is there

    if (x == "")
    {
        alert('Please enter a URL');
    }    
     else
    {
       window.open('http://' + x ,'_blank');
    }
}

</script> 
于 2013-05-22T10:15:47.467 に答える