首页 文章

使用JavaScript获取当前网址?

提问于
浏览
2607

我想要的只是获取网站URL . 不是从链接中获取的URL . 在页面加载中,我需要能够获取网站的完整,当前URL并将其设置为变量以便随意使用 .

20 回答

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

    上面的代码也可以帮助别人

  • 34

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

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

    使用: window.location.href .

    如上所述, document.URL doesn't update 更新 window.location 时 . 见MDN .

  • 0

    To get the path, you can use:

    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 URL
    
  • 3233

    打开 Developer Tools ,在 console 中输入以下内容并按 Enter .

    window.location
    

    例如:下面是当前页面上结果的屏幕截图 .

    从这里 grab 你需要的东西 . :)

  • 9

    获取当前位置对象的方法是 window.location .

    将此与 document.location 进行比较, document.location 最初仅将当前URL作为字符串返回 . 可能为避免混淆, document.locationdocument.URL 取代 .

    而且,所有现代浏览器都将 document.location 映射到 window.location .

    实际上,对于跨浏览器的安全性,您应该使用 window.location 而不是 document.location .

  • 29
    • 使用 window.location.href 获取完整的URL .

    • 使用 window.location.pathname 获取离开主机的URL .

  • 7
    location.origin+location.pathname+location.search+location.hash;
    
  • 26

    URL Info Access

    JavaScript为您提供了许多方法来检索和更改当前URL,该URL显示在浏览器的地址栏中 . 所有这些方法都使用 Location 对象,它是 Window 对象的属性 . 您可以创建具有当前URL的新 Location 对象,如下所示:

    var currentLocation = window.location;
    

    Basic URL Structure

    <protocol>//<hostname>:<port>/<pathname><search><hash>
    
    • protocol: 指定用于访问Internet上资源的协议名称 . (HTTP(不使用SSL)或HTTPS(使用SSL))

    • hostname: 主机名指定拥有该资源的主机 . 例如, www.stackoverflow.com . 服务器使用主机名提供服务 .

    • port: 用于识别Internet或其他网络消息到达服务器时要转发到的特定进程的端口号 .

    • pathname: 该路径提供有关Web客户端要访问的主机中的特定资源的信息 . 例如, /index.html .

    • query: 查询字符串在路径组件后面,并提供资源可用于某种目的的信息字符串(例如,作为搜索的参数或要处理的数据) .

    • hash: URL的锚点部分包括井号(#) .

    使用这些 Location 对象属性,您可以访问所有这些URL组件以及它们可以设置或返回的内容:

    • href - 整个网址

    • protocol - URL的协议

    • host - URL的主机名和端口

    • hostname - URL的主机名

    • port - 服务器用于URL的端口号

    • pathname - URL的路径名

    • search - URL的查询部分

    • hash - URL的锚点部分

    我希望你得到答案..

  • 14

    同样的问题has been asked less than 24 hours ago . 引用自己:

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

  • 0

    使用:

    window.location.href
    

    正如评论中所指出的那样,下面这一行有效,但是对于Firefox来说也是如此 .

    document.URL;
    

    URL of type DOMString, readonly .

  • 0

    Getting the current URL with JavaScript :

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

  • 18

    Adding result for quick reference

    window.location;

    Location {href: "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript",
     ancestorOrigins: DOMStringList,
     origin: "https://stackoverflow.com",
     replace: ƒ, assign: ƒ, …}
    

    document.location

    Location {href: "https://stackoverflow.com/questions/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

    "https://stackoverflow.com/questions/1034621/get-the-current-url-with-javascript"
    

    location.hostname

    "stackoverflow.com"
    
  • 14

    首先检查页面是否在浏览器中完全加载,然后调用一个带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 );
    });
    
  • 7

    获取当前页面URL:

    window.location.href
    
  • 4

    您可以get the current URL location with a hash tag使用:

    JavaScript:

    // Using href
     var URL = window.location.href;
    
     // Using path
     var URL = window.location.pathname;
    

    jQuery

    $(location).attr('href');
    
  • 4

    好的,使用纯JavaScript可以轻松获取当前页面的 full URL . 例如,在此页面上尝试此代码:

    window.location.href;
    // use it in 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.locatation.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
    
  • 0

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

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

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

  • 553

    如果你指的是具有 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="" 如上所述 .

  • 0

    对于包含查询字符串的完整URL:

    document.location.toString().toLowerCase();
    

    对于主机URL:

    window.location
    

相关问题