2

Trying to make a simple HTML page with a some links on it, but trying to make it so it detects you are either local or using the SSH Port Forwarding.

Some info;

URL I use when accessing the webpage Locally : 192.168.3.5/www/index.html

URL I Use when Using SSH Port Forwarding : 127.0.0.1:9009/www/index.html

This is the full code I am using in my index.html page....

<HTML>
- SABNZBD : Pick <A href="#" onclick="javascript:window.location.port=8080">LOCAL</a> or <A href="#" onclick="javascript:window.location.port=9001">REMOTE</a> Connection.
</HTML>

If I use this code;

 onclick="javascript:window.location.port=9001"

It sort of works, it returns

http://127.0.0.1:9001/www/#

I really want it to return just this :

http://127.0.0.1:9001/

Is there a way I could use the below code ? So it strips the pathname and just uses the hostname and the portname that I have specified ? As I can't seem to get it to work.

onclick="javascript:window.location.hostname && window.location.port=9001"

Thx Matt.

4

2 回答 2

3

以下应该做你需要的,在连接字符串时你不应该+使用&&

window.location = window.location.protocol + 
   '//' + window.location.hostname + ':9001';

您可以在处理程序中使用上述onclick内容:

onclick="window.location = ..."

您无需指定onclick="javascript:",因为这是默认情况下假定的。

此外,与其使用内联点击处理程序,不如使用以下内容:

关于如何同时使用以上两者(现代浏览器的 addEventListener,IE 的 attachEvent)

或者,如果您想让生活更简单,您可以使用自动处理这些浏览器差异的 JavaScript 库。像 jQuery。

$('.class_on_first_link').click(function(){
  window.location = window.location.protocol + 
   '//' + window.location.hostname + ':8080';
});

$('.class_on_second_link').click(function(){
  window.location = window.location.protocol + 
   '//' + window.location.hostname + ':9001';
});

更新

我上面省略号的意思是:

<a href="#" onclick="window.location = window.location.protocol + 
   '//' + window.location.hostname + ':9001';">Port 9001</a>
<a href="#" onclick="window.location = window.location.protocol + 
   '//' + window.location.hostname + ':8080';">Port 8080</a>
于 2012-10-30T00:33:25.183 回答
0

设置pathname也应该可以解决问题

onclick="window.location.port=9001;window.location.pathname='/'"
于 2012-10-30T00:36:13.490 回答