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


当前回答

如果您引用的是具有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=“”即可。

其他回答

在jstl中,我们可以使用pageContext.request.contextPath访问当前的URL路径。如果要进行Ajax调用,请使用以下URL。

url = "${pageContext.request.contextPath}" + "/controller/path"

示例:对于页面http://stackoverflow.com/posts/36577223这将给http://stackoverflow.com/controller/path.

如果您引用的是具有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=“”即可。

对于那些想要一个实际的URL对象的人,可能是一个以URL为参数的实用程序:

const url = new URL(window.location.href)

https://developer.mozilla.org/en-US/docs/Web/API/URL

对于带有查询字符串的完整URL:

document.location.toString()

对于主机URL:

window.location
var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
       currentPageUrlIs = this.href.toString().toLowerCase(); 
}else{ 
       currentPageUrlIs = document.location.toString().toLowerCase();
}

以上代码也可以帮助某人