2009-04-22 116 views
5

我已經去了我的JavaScript中的絕對URL,我已經硬編碼爲window.location。

我不想在每次測試我的應用程序時改變這一點。在PHP中,我將通過測試$ _SERVER [「HTTP_HOST」]變量來了解我所在的服務器,並相應地進行調整。但是,我對Java並不熟悉,我在想它是否有類似的方法?或者,如果甚至JavaScript有類似的方法?

的代碼如下:

var url = "http://172.17.1.107/store/results/index.jsp"; 
window.location = url; 

我想要做的是:

var server = [something that returns just 172.17.1.107 (with or without the http:// is fine)] 
var url = "http://" + server + "/store/results/index.jsp"; 
window.location = url; 

在PHP我剛做這個:

var server = <?= $_SERVER["HTTP_HOST"] ?> 
var url = "http://" + server + "/store/results/index.php"; 
window.location = url; 

任何想法?我想我正在假設你必須做一個絕對URL來改變JavaScript中當前窗口的位置。如果還有其他方法可以在沒有絕對URL的情況下更改JavaScript的窗口位置,請隨時提供。

在此先感謝...

回答

7

你需要的是:

request.getServerName() 

一個例子:

<% 
String path = request.getContextPath(); 
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 
%> 
-1

你真的應該尋找這一點,但在JSP是:

request.getRemoteHost() 
2

的Javascript:

var server = window.location.hostname; 
4

location對象has several properties,和一個你想要的是hostname

或者,您可以選擇使用根相對URL,只需設置pathname屬性,而不是混淆主機業務!

location.pathname = "/store/results/index.jsp"; 
-3

也許這將有助於。

它會用任何或任何東西來代替你想要的任何詞。它也適用於您的請求。

var str = "Visit Microsoft!"; 
var res = str.replace("Microsoft", "W3Schools"); 
+1

是的,水是藍色的。 HTF是這個相關的! – 2016-04-08 11:20:07