How to get base url with jquery or javascript?

后端 未结 24 1269
忘掉有多难
忘掉有多难 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:50

    Easy

    $('<img src=>')[0].src
    

    Generates a img with empty src-name forces the browser to calculate the base-url by itself, no matter if you have /index.html or anything else.

    0 讨论(0)
  • 2020-12-12 14:51

    I am surprised that non of the answers consider the base url if it was set in <base> tag. All current answers try to get the host name or server name or first part of address. This is the complete logic which also considers the <base> tag (which may refer to another domain or protocol):

    function getBaseURL(){
      var elem=document.getElementsByTagName("base")[0];
      if (typeof(elem) != 'undefined' && elem != null){
         return elem.href;
      }
      return window.location.origin;
    }
    

    Jquery format:

    function getBaseURL(){
      if ($("base").length){
         return $("base").attr("href");
      }
      return window.location.origin;
    }
    

    Without getting involved with the logic above, the shorthand solution which considers both <base> tag and window.location.origin:

    Js:

    var a=document.createElement("a");
    a.href=".";
    var baseURL= a.href;
    

    Jquery:

    var baseURL= $('<a href=".">')[0].href
    

    Final note: for a local file in your computer (not on a host) the window.location.origin only returns the file:// but the sorthand solution above returns the complete correct path.

    0 讨论(0)
  • 2020-12-12 14:52

    You mentioned that the example.com may change so I suspect that actually you need the base url just to be able to use relative path notation for your scripts. In this particular case there is no need to use scripting - instead add the base tag to your header:

    <head>
      <base href="http://www.example.com/">
    </head>
    

    I usually generate the link via PHP.

    0 讨论(0)
  • 2020-12-12 14:53
    window.location.origin+"/"+window.location.pathname.split('/')[1]+"/"+page+"/"+page+"_list.jsp"
    

    almost same as Jenish answer but a little shorter.

    0 讨论(0)
  • 2020-12-12 14:53

    I was just on the same stage and this solution works for me

    In the view

    <?php
        $document = JFactory::getDocument();
    
        $document->addScriptDeclaration('var base = \''.JURI::base().'\'');
        $document->addScript('components/com_name/js/filter.js');
    ?>
    

    In js file you access base as a variable for example in your scenario:

    console.log(base) // will print
    // http://www.example.com/
    // http://localhost/example
    // http://www.example.com/sub/example
    

    I do not remember where I take this information to give credit, if I find it I will edit the answer

    0 讨论(0)
  • 2020-12-12 14:54

    Here's something quick that also works with file:// URLs.

    I came up with this one-liner:

    [((1!=location.href.split(location.href.split("/").pop())[0].length?location.href.split(location.href.split("/").pop())[0]:(location.protocol,location.protocol+"//" + location.host+"/"))).replace(location.protocol+"//"+location.protocol+"//"+location.protocol+"://")]
    
    0 讨论(0)
提交回复
热议问题