如何在jQuery中获取基本路径? [英] How to get the base path in jQuery?

查看:374
本文介绍了如何在jQuery中获取基本路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

window.location可以正常工作,但可以像http://domain.xyz/punch/lines一样返回完整的绝对路径.但是我只需要http://domain.xyz/.我如何仅提取第一部分?我该如何使它变得动态,我的意思是即使子目录路径变长也要始终保持不变?

window.locationworks fine, but returns me the whole, absolute path, like http://domain.xyz/punch/lines. But I only need http://domain.xyz/. How can I extract only that first part? And how can I make that dynamic, I mean to be always the same even when the subdirectory path gets longer?

推荐答案

您可以分别获取协议和主机,然后加入它们以获取所需的信息

You can get the protocol and the host separately, and then join them to get what you need

window.location.protocol + "//" + window.location.host + "/"

作为旁注,window.location.pathname将包含路径.

As a sidenote, window.location.pathname would contain the path.

这篇关于如何在jQuery中获取基本路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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