超链接中不同端口号的相对URL?

编程入门 行业动态 更新时间:2024-10-26 00:18:29
本文介绍了超链接中不同端口号的相对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中测试

小提琴: jsfiddle/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: jsfiddle/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?

本文发布于:2023-08-07 14:21:04,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:端口号   超链接   URL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!