Is there any method to get the URL without query string?

后端 未结 14 1225
天涯浪人
天涯浪人 2020-11-28 19:22

I have a URL like http://localhost/dms/mduserSecurity/UIL/index.php?menu=true&submenu=true&pcode=1235.

I want to get the URL without the query s

相关标签:
14条回答
  • 2020-11-28 19:46
    location.toString().replace(location.search, "")
    
    0 讨论(0)
  • 2020-11-28 19:47

    Here's an approach using the URL() interface:

    new URL(location.pathname, location.href).href
    
    0 讨论(0)
  • 2020-11-28 19:47

    To get every part of the URL except for the query:

    var url = (location.origin).concat(location.pathname).concat(location.hash);
    

    Note that this includes the hash as well, if there is one (I'm aware there's no hash in your example URL, but I included that aspect for completeness). To eliminate the hash, simply exclude .concat(location.hash).

    It's better practice to use concat to join Javascript strings together (rather than +): in some situations it avoids problems such as type confusion.

    0 讨论(0)
  • 2020-11-28 19:49

    Read about Window.location and the Location interface:

    var url = [location.protocol, '//', location.host, location.pathname].join('');
    
    0 讨论(0)
  • 2020-11-28 19:50

    Try:

    document.location.protocol + '//' +
    document.location.host +
    document.location.pathname;
    

    (NB: .host rather than .hostname so that the port gets included too, if necessary)

    0 讨论(0)
  • 2020-11-28 19:51

    How about this: location.href.slice(0, - ((location.search + location.hash).length))

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