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.
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.
Share Improve this question asked Oct 30, 2012 at 0:09 Matt.Matt. 1,0331 gold badge13 silver badges20 bronze badges2 Answers
Reset to default 3The following should do what you need, when concatenating strings you should use +
not &&
window.location = window.location.protocol +
'//' + window.location.hostname + ':9001';
You can use the above in an onclick
handler:
onclick="window.location = ..."
You don't need to specify onclick="javascript:"
as this is assumed by default.
Also, rather than using inline click handlers, it would be better to use the following:
- https://developer.mozilla/en-US/docs/DOM/element.addEventListener
- http://msdn.microsoft./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):
- MSIE and addEventListener Problem in Javascript?
Or if you wanted to make life simpler you could just use a JavaScript library that handles these browser differences automatically. Like 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>
Setting the pathname
as well should do the trick
onclick="window.location.port=9001;window.location.pathname='/'"