javascript 使用带有简单 html 链接的 window.location
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13131464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
using window.location with a simple html link
提问by Matt.
Trying to make a simpleHTML 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.
试图制作一个简单的HTML 页面,上面有一些链接,但试图制作它以便检测到您是本地的还是使用 SSH 端口转发。
Some info;
一些信息;
URL I use when accessing the webpage Locally : 192.168.3.5/www/index.html
我在本地访问网页时使用的 URL:192.168.3.5/www/index.html
URL I Use when Using SSH Port Forwarding : 127.0.0.1:9009/www/index.html
我在使用 SSH 端口转发时使用的 URL:127.0.0.1:9009/www/index.html
This is the full code I am using in my index.html page....
这是我在 index.html 页面中使用的完整代码......
<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.
谢谢马特。
回答by Pebbl
The following should do what you need, when concatenating strings you should use +
not &&
以下应该做你需要的,在连接字符串时你应该使用+
not&&
window.location = window.location.protocol +
'//' + window.location.hostname + ':9001';
You can use the above in an onclick
handler:
您可以在onclick
处理程序中使用上述内容:
onclick="window.location = ..."
You don't need to specify onclick="javascript:"
as this is assumed by default.
您不需要指定,onclick="javascript:"
因为这是默认情况下的假设。
Also, rather than using inline click handlers, it would be better to use the following:
此外,与其使用内联点击处理程序,不如使用以下内容:
- https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener
- http://msdn.microsoft.com/en-us/library/ie/ms536343%28v=vs.85%29.aspx
- https://developer.mozilla.org/en-US/docs/DOM/element.addEventListener
- http://msdn.microsoft.com/en-us/library/ie/ms536343%28v=vs.85%29.aspx
For how to use both the above together (addEventListener for the modern browsers, attachEvent for IE):
关于如何同时使用以上两者(现代浏览器为 addEventListener,IE 为 attachEvent):
Or if you wanted to make life simpler you could just use a JavaScript library that handles these browser differences automatically. Like jQuery.
或者,如果您想让生活更简单,您可以使用一个 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';
});
update
更新
What I meant by the ellipsis above was:
我上面省略号的意思是:
<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>
回答by wiill
Setting the pathname
as well should do the trick
设置pathname
也应该可以解决问题
onclick="window.location.port=9001;window.location.pathname='/'"