Ciaran McNulty Ciaran McNulty - 2 years ago 676
HTML Question

Relative URL to a different port number in a hyperlink?

Is there a way without Javascript / server-side scripting to link to a different port number on the same box, if I don't know the hostname?


<a href=":8080">Look at the other port</a>

(This example does't work as it'll just treat :8080 as a string I want to navigate to)

Answer Source

It would be nice if this could work, and I don't see why not because : is a reserved character for port separation inside the URI component, so the browser could realistically interpret this as a port relative to this URL, but unfortunately it doesn't and there's no way for it to do that.

You'll therefore need Javascript to do this;

// delegate event for performance, and save attaching a million events to each anchor
document.addEventListener('click', function(event) {
  var target =;
  if (target.tagName.toLowerCase() == 'a')
      var port = target.getAttribute('href').match(/^:(\d+)(.*)/);
      if (port)
         target.href = port[2];
         target.port = port[1];
}, false);

Tested in Firefox 4


Update: Bug fixed for appending port to end of url and also added support for relative and absolute urls to be appended to the end:

<a href=":8080/test/blah">Test absolute</a>
<a href=":7051./test/blah">Test relative</a>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download