Get the current URL with JavaScript?

后端 未结 23 1966
梦谈多话
梦谈多话 2020-11-21 07:08

All I want is to get the website URL. Not the URL as taken from a link. On the page loading I need to be able to grab the full, current URL of the website and set it as a va

相关标签:
23条回答
  • 2020-11-21 07:41

    For complete URL with query strings:

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

    For host URL:

    window.location
    
    0 讨论(0)
  • 2020-11-21 07:43

    For those who want an actual URL object, potentially for a utility which takes URLs as an argument:

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

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

    0 讨论(0)
  • 2020-11-21 07:44
    var currentPageUrlIs = "";
    if (typeof this.href != "undefined") {
           currentPageUrlIs = this.href.toString().toLowerCase(); 
    }else{ 
           currentPageUrlIs = document.location.toString().toLowerCase();
    }
    

    The above code can also help someone

    0 讨论(0)
  • 2020-11-21 07:47

    The way to get the current location object is window.location.

    Compare this to document.location, which originally only returned the current URL as a string. Probably to avoid confusion, document.location was replaced with document.URL.

    And, all modern browsers map document.location to window.location.

    In reality, for cross-browser safety, you should use window.location rather than document.location.

    0 讨论(0)
  • 2020-11-21 07:50

    URL Info Access

    JavaScript provides you with many methods to retrieve and change the current URL, which is displayed in the browser's address bar. All these methods use the Location object, which is a property of the Window object. You can create a new Location object that has the current URL as follows:

    var currentLocation = window.location;
    

    Basic URL Structure

    <protocol>//<hostname>:<port>/<pathname><search><hash>
    
    • protocol: Specifies the protocol name be used to access the resource on the Internet. (HTTP (without SSL) or HTTPS (with SSL))

    • hostname: Host name specifies the host that owns the resource. For example, www.stackoverflow.com. A server provides services using the name of the host.

    • port: A port number used to recognize a specific process to which an Internet or other network message is to be forwarded when it arrives at a server.

    • pathname: The path gives info about the specific resource within the host that the Web client wants to access. For example, /index.html.

    • search: A query string follows the path component, and provides a string of information that the resource can utilize for some purpose (for example, as parameters for a search or as data to be processed).

    • hash: The anchor portion of a URL, includes the hash sign (#).

    With these Location object properties you can access all of these URL components and what they can set or return:

    • href - the entire URL
    • protocol - the protocol of the URL
    • host - the hostname and port of the URL
    • hostname - the hostname of the URL
    • port - the port number the server uses for the URL
    • pathname - the path name of the URL
    • search - the query portion of the URL
    • hash - the anchor portion of the URL

    I hope you got your answer..

    0 讨论(0)
  • 2020-11-21 07:51

    if you are referring to a specific link that has an id this code can help you.

    $(".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+"");
            }
        });
    });
    

    I am using ajax here to submit an id and redirect the page using window.location.replace. just add an attribute id="" as stated.

    0 讨论(0)
提交回复
热议问题