我只想获取网站URL。不是从链接获取的URL。在页面加载时,我需要能够获取网站的完整、当前URL,并将其设置为一个变量,以便根据需要进行处理。


当前回答

要获取路径,可以使用:

console.log('document.location',document.location.href);console.log('location.pathname',window.location.pathname);//仅返回路径console.log('location.href',window.location.href);//返回完整URL

其他回答

打开开发人员工具,在控制台中键入以下内容,然后按Enter键。

window.location

下面是当前页面上的结果截图。

从这里获取您需要的信息。:)

使用window.location对与当前帧关联的位置对象进行读写访问。如果您只想以只读字符串形式获取地址,可以使用document.URL,它应该包含与window.location.href相同的值。

使用JavaScript获取当前URL:

window.location.toString();窗口.location.href

您可以通过location.href获取当前页面的完整链接要获取当前控制器的链接,请使用:

location.href.substring(0, location.href.lastIndexOf('/'));

如果您引用的是具有id的特定链接,则此代码可以帮助您。

$(".disapprove").click(function(){
    var id = $(this).attr("id");

    $.ajax({
        url: "<?php echo base_url('index.php/sample/page/"+id+"')?>",
        type: "post",
        success:function()
        {
            alert("The Request has been Disapproved");
            window.location.replace("http://localhost/sample/page/"+id+"");
        }
    });
});

我在这里使用ajax来提交一个id,并使用window.location.replace重定向页面。只需添加一个属性id=“”即可。