Get current URL with jQuery?

后端 未结 30 2218
你的背包
你的背包 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:19
    // get current URL
    
    $(location).attr('href');
    var pathname = window.location.pathname;
    alert(window.location);
    
    0 讨论(0)
  • 2020-11-22 03:22

    In jstl we can access current url path using pageContext.request.contextPath, If you want to do a ajax call,

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

    Ex: in the page http://stackoverflow.com/questions/406192 this will give http://stackoverflow.com/controller/path

    0 讨论(0)
  • 2020-11-22 03:24
    var newURL = window.location.protocol + "//" + window.location.host + "/" + window.location.pathname;
    
    0 讨论(0)
  • 2020-11-22 03:25

    To get the path, you can use:

    var pathname = window.location.pathname; // Returns path only (/path/example.html)
    var url      = window.location.href;     // Returns full URL (https://example.com/path/example.html)
    var origin   = window.location.origin;   // Returns base URL (https://example.com)
    
    0 讨论(0)
  • 2020-11-22 03:25

    This will return the absolute URL of the current page using JavaScript/jQuery.

    • document.URL

    • $("*").context.baseURI

    • location.href

    0 讨论(0)
  • 2020-11-22 03:26

    Use window.location.href. This will give you the complete URL.

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