2016-09-27 82 views
0

我一直在尋找以下兩種思路相結合,因爲我需要去根據當前的URL插入網址到地址欄

http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_loc_hostname從我有個親戚網址:

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

而且然後:

var newUrl = ""; 
window.location = newUrl/x; 

所以,我可以在頁面上的按鈕來把我從domain1.suffix

domain2.suffix/domain2.suffix有關該域名的網頁

任何人都可以幫我把這件作品放在一起嗎?

+1

難道僅僅是你需要幫助的字符串連接? 'window.location = newUrl +'/'+ x;'。簡單地使用'/'將嘗試執行兩個變量的分割。 –

+0

使用concat(),你可以組合字符串 –

+0

你到底有什麼困難? – Liam

回答

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> 
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> 
+0

這似乎很好 - 非常感謝。我也喜歡concat()和newUrl +'/'+ x評論!謝謝大家 :) – sircles