How to get base url with jquery or javascript?

后端 未结 24 1266
忘掉有多难
忘掉有多难 2020-12-12 13:59

In joomla php there I can use $this->baseurl to get the base path, but I wanted to get the base path in jquery.

The base path may be any of the follo

相关标签:
24条回答
  • 2020-12-12 14:32

    This is not possible from javascript, because this is a server-side property. Javascript on the client cannot know where joomla is installed. The best option is to somehow include the value of $this->baseurl into the page javascript and then use this value (phpBaseUrl).

    You can then build the url like this:

    var loc = window.location;
    var baseUrl = loc.protocol + "//" + loc.hostname + (loc.port? ":"+loc.port : "") + "/" + phpBaseUrl;
    
    0 讨论(0)
  • 2020-12-12 14:33

    Split and join the URL:

    const s = 'http://free-proxy.cz/en/abc'
    console.log(s.split('/').slice(0,3).join('/'))
    
    0 讨论(0)
  • 2020-12-12 14:35

    You can easily get it with:

    var currentUrl = window.location.href;
    

    or, if you want the original URL, use:

    var originalUrl = window.location.origin;
    
    0 讨论(0)
  • 2020-12-12 14:37

    This will get base url

    var baseurl = window.location.origin+window.location.pathname;
    
    0 讨论(0)
  • 2020-12-12 14:37

    Had the same issue a while ago, my problem was, I simply needed the base url. There are a lot of detailed options here but to get around this, simply use the window.location object. Actually type this in the browser console and hit enter to select your options there. Well for my case it was simply:

    window.location.origin
    
    0 讨论(0)
  • 2020-12-12 14:39

    This one will help you...

    var getUrl = window.location;
    var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];
    
    0 讨论(0)
提交回复
热议问题