使用 window.location.replace 有没有办法更改端口?

发布于 2024-11-30 16:44:53 字数 152 浏览 0 评论 0原文

使用 js 使用各种方法/赋值来更改浏览器 url。一切正常,但虽然我可以读取当前端口,但我似乎无法设置新端口 - 这可能吗? (我将浏览器从一个站点上的端口 80 重定向到另一个站点上的 node.js 端口,并且不想在节点盒上使用代理)。

提前干杯。

Using the various methods/assignments to change the browser url using js. All work fine but whilst I can read the current port, I can't seem to set the new port - is this possible ? (I'm redirecting the browser from port 80 on one site to a node.js port on another site and don't want to use a proxy on the node box).

Cheers in advance.

N

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请远离我 2024-12-07 16:44:53

确实在我的浏览器中有效,是的,我在重定向后在地址栏中看到了端口号:

window.location.replace( 'http://somedomain.com:1234/' );

这是一个也有效的替代方案:

window.location.port = 1234;

This does work in my browser and yes I see the port number in the address bar after redirection:

window.location.replace( 'http://somedomain.com:1234/' );

and here is a alternative which also works:

window.location.port = 1234;
还给你自由 2024-12-07 16:44:53
window.location = "http://stackoverflow.com:1234"

对我来说“有效”(例如,无法连接!)

window.location = "http://stackoverflow.com:1234"

"works" (as in, fails to connect!) for me

年华零落成诗 2024-12-07 16:44:53

好吧,我可能是个白痴;-) 谢谢没人和布莱恩 - 这些建议不起作用,但我解决了问题。进行重定向的网页上绝对没有其他代码,因此我对它失败的原因感到有点困惑。不过,我似乎添加了一个

<META HTTP-EQUIV=REFRESH 

未启用 JavaScript 的重定向。我未能在此处添加端口,因此是此代码执行重定向,而不是 window.location 内容。在此处添加端口解决了问题,并且可能是执行我想做的事情的正确位置。

Ok I may have been an idiot ;-) Thanks nobody and Brian - those suggestions didn't work but I solved the problem. The webpage doing the redirection had absolutely no other code on it so I was at a bit of a loss as to why it was failing. However it seems I had added a

<META HTTP-EQUIV=REFRESH 

for the redirection where javascript wasn't enabled. I had failed to add the port here and so it was this code that was doing the redirection, not the window.location stuff. Adding the port here solved the problem and is probably the right place to do what I was trying to do.

N

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文