将url插入地址栏

时间:2016-09-27 06:09:17

标签: javascript html url

我一直在考虑将以下两个想法结合起来,因为我需要根据当前网址转到相对网址

我有{p> http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_loc_hostname

profile-it

然后:

<script>
    function myFunction() {
        var x = location.hostname;
        document.getElementById("demo").innerHTML= x;
    }
</script>

这样我就可以在页面上有一个按钮从domain1.suffix

中获取

var newUrl = ""; window.location = newUrl/x; 有关该域名的网页

任何人都可以帮我把它拼凑起来吗?

2 个答案:

答案 0 :(得分:1)

试试此代码

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script>
        function GetHost()
        {
            var protocol = "http://";
            var x = location.hostname;
            window.open((protocol + x));
            document.getElementById("demo").innerText = protocol + x;

        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input type="button" value="Test It" onclick="GetHost()" />
        <p id="demo"></p>
    </div>
    </form>
</body>
</html>

答案 1 :(得分:0)

试试这个:

<html>
    <head>
        <script>
            function openWin() {
                var x = "http://" + location.hostname;
                window.open(x);
            }
        </script>
    </head>
    <body>
        <form>
            <input type="button" value="Open Window" onclick="openWin()">
        </form>
    </body>
</html>