Get current URL with jQuery?

后端 未结 30 2236
你的背包
你的背包 2020-11-22 02:44

I am using jQuery. How do I get the path of the current URL and assign it to a variable?

Example URL:

http://localhost/menuname.de?foo=bar&nu         


        
30条回答
  •  广开言路
    2020-11-22 03:18

    java-script provides many methods to retrieve current URL which is displayed in browser's address bar.

    Test URL :

    http://
    stackoverflow.com/questions/5515310/get-current-url-with-jquery/32942762
    ?
    rq=1&page=2&tab=active&answertab=votes
    #
    32942762
    
    resourceAddress.hash();
    console.log('URL Object ', webAddress);
    console.log('Parameters ', param_values);
    

    Function:

    var webAddress = {};
    var param_values = {};
    var protocol = '';
    var resourceAddress = {
    
        fullAddress : function () {
            var addressBar = window.location.href;
            if ( addressBar != '' && addressBar != 'undefined') {
                webAddress[ 'href' ] = addressBar;
            }
        },
        protocol_identifier : function () { resourceAddress.fullAddress();
    
            protocol = window.location.protocol.replace(':', '');
            if ( protocol != '' && protocol != 'undefined') {
                webAddress[ 'protocol' ] = protocol;
            }
        },
        domain : function () {      resourceAddress.protocol_identifier();
    
            var domain = window.location.hostname;
            if ( domain != '' && domain != 'undefined' && typeOfVar(domain) === 'string') {
                webAddress[ 'domain' ] = domain;
                var port = window.location.port;
                if ( (port == '' || port == 'undefined') && typeOfVar(port) === 'string') {
                    if(protocol == 'http') port = '80';
                    if(protocol == 'https') port = '443';           
                }
                webAddress[ 'port' ] = port;
            }
        },
        pathname : function () {        resourceAddress.domain();
    
            var resourcePath = window.location.pathname;
            if ( resourcePath != '' && resourcePath != 'undefined') {
                webAddress[ 'resourcePath' ] = resourcePath;
            }
        },
        params : function () {      resourceAddress.pathname();
    
            var v_args = location.search.substring(1).split("&");
    
            if ( v_args != '' && v_args != 'undefined')
            for (var i = 0; i < v_args.length; i++) {
                var pair = v_args[i].split("=");
    
                if ( typeOfVar( pair ) === 'array' ) {
                    param_values[ decodeURIComponent( pair[0] ) ] = decodeURIComponent( pair[1] );
                }
            }
            webAddress[ 'params' ] = param_values;
        },
        hash : function () {        resourceAddress.params();
    
            var fragment = window.location.hash.substring(1);
            if ( fragment != '' && fragment != 'undefined')
                webAddress[ 'hash' ] = fragment;        
        }
    };
    function typeOfVar (obj) {
          return {}.toString.call(obj).split(' ')[1].slice(0, -1).toLowerCase();
    }
    
    • protocol « Web-browsers use Internet Protocol by following some rules for communication between WebHosted Applications and Web Client(Browser). (http = 80, https (SSL) = 443, ftp = 21, etc.)

    EX: With default port numbers

    //:/
    https://en.wikipedia.org:443/wiki/Pretty_Good_Privacy
    http://stackoverflow.com:80/
    
    • (//) « Host is the name given to an end-point(machine on which resource lives) on the Internet. www.stackoverflow.com - DNS IP Address of an Application (OR) localhost:8080 - localhost

    Domain names are which you register by the rules and procedures of the Domain Name System(DNS) tree. DNS servers of someone who manages your domain with IP-Address for addressing purposes. In DNS server hierarchy the Root name of an stackoverlfow.com is com.

    gTLDs      - com « stackoverflow (OR) in « co « google
    

    Local system you have to maintain domain's which are not PUBLIC in Host Files. localhost.yash.com « localhsot - subdomain(web-server), yash.com - maindomain(Proxy-Server). myLocalApplication.com 172.89.23.777

    • (/) « The path gives info about the specific resource within the host that the Web client wants to access
    • (?) « An optional query is to pass a sequence of attribute–value pairs separated by a delimiter(&).
    • (#) « An optional fragment is often an id attribute of a specific element, and web browsers will scroll this element into view.

    If parameter has an Epoch ?date=1467708674 then use.

    var epochDate = 1467708674; var date = new Date( epochDate );
    

    URL


    Authentication url with username:password, If usernaem/password contains @ symbol
    like:

    Username = `my_email@gmail`
    Password = `Yash@777`
    

    then You need to URL encode the @ as %40. Refer...

    http://my_email%40gmail.com:Yash%40777@www.my_site.com
    

    encodeURI() (vs) encodeURIComponent() example

    var testURL = "http:my_email@gmail:Yash777@//stackoverflow.com?tab=active&page=1#32942762";
    
    var Uri = "/:@?&=,#", UriComponent = "$;+", Unescaped = "(-_.!~*')"; // Fixed
    var encodeURI_Str = encodeURI(Uri) +' '+ encodeURI( UriComponent ) +' '+ encodeURI(Unescaped);
    var encodeURIComponent_Str =  encodeURIComponent( Uri ) +' '+ encodeURIComponent( UriComponent ) +' '+ encodeURIComponent( Unescaped );
    console.log(encodeURI_Str, '\n', encodeURIComponent_Str);
    /*
     /:@?&=,# +$; (-_.!~*') 
     %2F%3A%40%3F%26%3D%2C%23 %2B%24%3B (-_.!~*')
    */
    

提交回复
热议问题