javascript - 用 location 在本地服务器设置跳转出错,为什么?
PHP中文网
PHP中文网 2017-04-10 16:18:01
[JavaScript讨论组]

前端新兵,今天遇到个问题,向各位大牛请教。

我有一个页面,地址为 http://192.168.3.197:8888/login.do?method=toLogin
在这个页面里有一个链接,我想点击这个链接,跳转到 http://192.168.3.197:8888/

我是这样写的:

$("#a").click(function(){
    window.location=location.protocol+location.host;
});

但是,点击之后,却跳转到了
http://192.168.3.197:8888/192.168.3.197:8888

这是为什么呢?以及要怎么写才能跳转到正确地址呢?

谢谢!

PHP中文网
PHP中文网

认证0级讲师

全部回复(3)
怪我咯

应该是location.href = location.protocol + '//' + location.host

完整的url里有'//'
protocol是协议
hostname是主机
port是端口

黄舟

如果是单一服务器跳转,直接使用相对路径吧:

location.href = '/login.do?method=toLogin';
大家讲道理

http://192.168.3.197:8888/login.do?method=toLogin
要跳转到
http://192.168.30.177:8888/

这个2个地址完全没有啊

$("#a").click(function(){
    window.location=‘http://192.168.30.177:8888’;
});

不就好了

还是说要跳到http://192.168.3.197:8888?

$("#a").click(function(){
    window.location=location.protocol+"//"+location.host+(location.port?(':'+location.port):'');
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号