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.

Matt.
  • 1,043
  • 1
  • 12
  • 20

2 Answers2

3

The 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:

For how to use both the above together (addEventListener for the modern browsers, attachEvent for IE):

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>
Community
  • 1
  • 1
Pebbl
  • 34,937
  • 6
  • 62
  • 64
  • How would I put that into an HTML file,
    ` SABNZBD : REMOTE Connection. ` ?? Anyway to have it so that the onclick specifies the port number?
    – Matt. Oct 31 '12 at 05:24
  • Sorry about above, new to stack... here is a more readable version, [PasteBin Code](http://pastebin.com/qDBc0QWD) – Matt. Oct 31 '12 at 05:32
  • @Matt I've updated my answer with a response to what you've asked. Rather than using pastebin, in future, just update your question with further information. Welcome to SO btw :) – Pebbl Oct 31 '12 at 10:17
  • BINGO, thx for that works nicely now. Will remember the tip about updating code. – Matt. Oct 31 '12 at 23:13
0

Setting the pathname as well should do the trick

onclick="window.location.port=9001;window.location.pathname='/'"
willtrnr
  • 431
  • 5
  • 15