how to get the base url in javascript

前端 未结 10 1837
一整个雨季
一整个雨季 2020-12-01 04:12

I am building a website with CodeIgniter, I have various resources that I load with the base_url helper function like this

10条回答
  •  情歌与酒
    2020-12-01 04:40

    Base URL in JavaScript

    Here is simple function for your project to get base URL in JavaScript.

    // base url
    function base_url() {
        var pathparts = location.pathname.split('/');
        if (location.host == 'localhost') {
            var url = location.origin+'/'+pathparts[1].trim('/')+'/'; // http://localhost/myproject/
        }else{
            var url = location.origin; // http://stackoverflow.com
        }
        return url;
    }
    

提交回复
热议问题