超链接中不同端口号的相对URL? [英] Relative URL to a different port number in a hyperlink?

查看:192
本文介绍了超链接中不同端口号的相对URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我不知道主机名,有没有一种方法没有JavaScript /服务器端脚本链接到同一个盒子上的不同端口号?



例如:

 < a href =:8080>查看其他端口< / a> 

(这个例子没有用,因为它只会把8080当作我想要的字符串导航到)

解决方案

如果这可以起作用,那就好了,我不明白为什么不是因为是URI组件内部用于端口分离的保留字符,因此浏览器可以真实地将其解释为相对于此URL的端口,但不幸的是,它不会并且无法做到这一点。



因此,您需要使用Javascript来完成此操作;

  //为事件委托事件,并保存附加一百万个事件给每个锚点
document.addEventListener('click',function(event){
var target = event.target;
if(target.tagName.toLowerCase()=='a')
{
var port = target.getAttribute('href')。match(/ ^ :( \d +)(。*) /);
if(port)
{
target.href = port [2];
target.port = port [1];
}
}
},false);

在Firefox 4中测试

小提琴: http://jsfiddle.net/JtF39/79/






更新:修复了将端口附加到网址结尾的问题,并且还增加了对相对和绝对网址的支持将被追加到最后:

 < a href =:8080 / test / blah>测试绝对值< /一个> 
< a href =:7051./test/blah\">相对测试< / a>


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?

e.g.:

<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)

解决方案

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 = event.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

Fiddle: http://jsfiddle.net/JtF39/79/


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>

这篇关于超链接中不同端口号的相对URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆