js怎么获取url


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

php thinkphp JavaScript

Enses 9 years, 6 months ago

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

由比ケ浜結衣 answered 9 years, 6 months ago

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


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

当然你也可以自己用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

人类也是动物 answered 9 years, 6 months ago

Your Answer