2009-04-22 120 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