Wednesday, September 23, 2015

how to get site base url in javascript

js dotoroo base_url aa avah neg iimerhuu arga baij boloh yum
<?php echo base_url(); ?>
or
base_url: (function () {
        var myScripts = document.getElementsByTagName('script');
        var myScript = myScripts[myScripts.length - 1];
        var myArgs = myScript.src.split('?');
        var myModule = '';
        if (myArgs.length > 1) {
            myModule = myArgs[1];
        }
        var host = window.location.hostname;
        var baseurl = window.location.protocol + '//' + host;
        var fip = host.split('.')[0];
        var isip = (0 < fip && fip < 256);
        if (isip === false && host.indexOf('.') > 0) {
            baseurl = baseurl + (myModule.length > 0 ? '/' + myModule : '');
        } else {
            var jssrc = myArgs[0];
            var jssrc_seg = jssrc.split('/');
            var rootfolder = jssrc_seg[3];
            baseurl = baseurl + (rootfolder.length > 0 ? '/' + rootfolder : '');
        }
        return baseurl;
    })()

No comments: