Home  >  Article  >  Backend Development  >  javascript - js怎么获取url

javascript - js怎么获取url

WBOY
WBOYOriginal
2016-06-06 20:32:28954browse

因为是用thinkphp,在html处可以直接用 __ URL __ 替代模版,但在引入的JavaScript中使用不了,提交ajax时需要url:__ URL __/xxx,应该怎么获得这个 __ URL __。用js截取吗?

回复内容:

因为是用thinkphp,在html处可以直接用 __ URL __ 替代模版,但在引入的JavaScript中使用不了,提交ajax时需要url:__ URL __/xxx,应该怎么获得这个 __ URL __。用js截取吗?

试试用别的方法,比如说把这个url赋值给HTML中的某个元素的某个属性。

我用的是CI,没用thinkphp。通常我的做法是,

<code><script type="text/javascript">
    //默认配置url
    var base_url = '<?php echo $this->config->base_url(); ?>';
</script>
</code>

当然你也可以自己用js获取当前页面的相关url路径
设置或获取对象指定的文件名或路径。
alert(window.location.pathname)

设置或获取整个 URL 为字符串。

alert(window.location.href);
设置或获取与 URL 关联的端口号码。
alert(window.location.port)

设置或获取 URL 的协议部分。
alert(window.location.protocol)

设置或获取 href 属性中在井号“#”后面的分段。
alert(window.location.hash)

设置或获取 location 或 URL 的 hostname 和 port 号码。
alert(window.location.host)

设置或获取 href 属性中跟在问号后面的部分。
alert(window.location.search)

具体的属性api,你可以参阅这个链接
location

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn