我想要的只是获取网站URL。不是从链接获取的URL。在页面加载中,我需要能够获取网站的完整,当前URL,并将其设置为一个变量,以便我随意使用。
我想要的只是获取网站URL。不是从链接获取的URL。在页面加载中,我需要能够获取网站的完整,当前URL,并将其设置为一个变量,以便我随意使用。
Answers:
document.URL属性不会在a之后更新window.location为锚点(#),而window.location.href会更新。我在此处整理了一个实时演示:jsfiddle.net/PxgKy我没有测试过任何其他版本的Firefox。document.URL在Chrome 20和IE9中未发现使用问题。
                    window.location.host和window.location.href.toString().split(window.location.host)[1]
                    document.baseURI。基本上有3种方式来获得URL document.baseURI,document.URL,和location。
                    name="URL"则此属性将在document对象上被遮盖,并且代码将中断。在这种情况下,document.URL将改为引用DOM节点。最好像中使用全局对象的属性window.location.href。
                    URL信息访问
JavaScript为您提供了许多检索和更改当前URL的方法,该方法显示在浏览器的地址栏中。所有这些方法都使用Location对象,它是对象的属性Window。您可以创建一个Location具有当前URL 的新对象,如下所示:
var currentLocation = window.location;基本网址结构
<protocol>//<hostname>:<port>/<pathname><search><hash>protocol:协议名称,用于访问Internet上的资源。(HTTP(不带SSL)或HTTPS(带SSL))
hostname:主机名指定拥有资源的主机。例如,www.stackoverflow.com。服务器使用主机名提供服务。
端口:端口号,用于识别Internet或其他网络消息到达服务器时将转发到的特定进程。
pathname:路径提供有关Web客户端要访问的主机内特定资源的信息。例如,/index.html。
搜索:查询字符串位于路径部分之后,并提供资源可用于某些目的的信息字符串(例如,作为搜索的参数或要处理的数据)。
hash: URL的锚点部分,包括井号(#)。
使用这些Location对象属性,您可以访问所有这些URL组件以及它们可以设置或返回的内容:
我希望你能得到答案。
substring。但是,当您要使用重定向document.location = "/page.html";将重定向到根页面时可能很有用page.html
                    search,但是在下面的描述列表中,它称为query。也许可以和解,或者可以添加进一步的说明。
                    用于window.location对与当前框架关联的位置对象的读写访问。如果您只想以只读字符串的形式获取地址,则可以使用document.URL,其值应与相同window.location.href。
好的,使用纯JavaScript即可轻松获得当前页面的完整URL。例如,在此页面上尝试以下代码:
window.location.href;
// use it in the console of this page will return
// http://stackoverflow.com/questions/1034621/get-current-url-in-web-browser"
该
window.location.href属性返回当前页面的URL。
document.getElementById("root").innerHTML = "The full URL of this page is:<br>" + window.location.href;<!DOCTYPE html>
<html>
<body>
  <h2>JavaScript</h2>
  <h3>The window.location.href</h3>
  <p id="root"></p>
</body>
</html>同样值得一提的是:
如果您需要相对路径,只需使用window.location.pathname;
如果要获取主机名,可以使用window.location.hostname;
如果您需要单独获取协议,请使用 window.location.protocol
hash标签,则可以像这样获得它window.location.hash。所以window.location.href一次处理所有...基本上:
window.location.protocol + '//' + window.location.hostname + window.location.pathname + window.location.hash === window.location.href;
    //true
window如果已经在窗口范围内,也不需要使用...
因此,在这种情况下,您可以使用:
location.protocol
location.hostname
location.pathname
location.hash
location.href
要获取路径,可以使用:
console.log('document.location', document.location.href);
console.log('location.pathname',  window.location.pathname); // Returns path only
console.log('location.href', window.location.href); // Returns full URLwindow.location.href以获得完整的URL。window.location.pathname得到URL离开主机。您可以使用以下命令使用哈希标签获取当前URL位置:
JavaScript:
 // Using href
 var URL = window.location.href;
 // Using path
 var URL = window.location.pathname;jQuery的:
$(location).attr('href');var currentPageUrlIs = "";
if (typeof this.href != "undefined") {
       currentPageUrlIs = this.href.toString().toLowerCase(); 
}else{ 
       currentPageUrlIs = document.location.toString().toLowerCase();
}上面的代码也可以帮助某人
http://www.server.com/path/to/Script.php?option=A1B2C3,如果文件系统区分大小写(Linux / Unix),则不必Script.php和script.php相同。即使不区分大小写(Windows,某些Mac OS),?option=A1B2C3也不相同?option=a1b2c3,甚至?Option=A1B2C3。仅服务器区分大小写:www.server.com或www.SeRvEr.cOm相同。
                    添加结果以供快速参考
window.location;
 Location {href: "/programming/1034621/get-the-current-url-with-javascript",
 ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ, …}文件位置
  Location {href: "/programming/1034621/get-the-current-url-with-javascript", 
ancestorOrigins: DOMStringList,
 origin: "https://stackoverflow.com",
 replace: ƒ, assign: ƒ
, …}window.location.pathname
"/questions/1034621/get-the-current-url-with-javascript"window.location.href
"/programming/1034621/get-the-current-url-with-javascript"location.hostname
"stackoverflow.com"对于带有查询字符串的完整URL:
document.location.toString().toLowerCase();对于主机URL:
window.location在jstl中,我们可以使用来访问当前的URL路径pageContext.request.contextPath。如果要进行Ajax调用,请使用以下URL。
url = "${pageContext.request.contextPath}" + "/controller/path"示例:对于页面,http://stackoverflow.com/posts/36577223将给出http://stackoverflow.com/controller/path。
对于那些想要实际URL对象的人,可能是将URL作为参数的实用程序:
const url = new URL(window.location.href)您可以通过以下location.href
方式获取当前页面的完整链接,并获取当前控制器的链接,请使用:
location.href.substring(0, location.href.lastIndexOf('/'));使用JavaScript获取当前URL:
window.location.toString();
window.location.href
如果您指的是具有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=""按照说明添加属性。
首先检查页面是否已完全加载
browser,window.location.toString();
window.location.href然后调用一个带有url,URL变量并在控制台上打印的函数,
$(window).load(function(){
   var url = window.location.href.toString();
   var URL = document.URL;
   var wayThreeUsingJQuery = $(location).attr('href');
   console.log(url);
   console.log(URL);
   console.log(wayThreeUsingJQuery );
});