简体   繁体   中英

Get Base URL from Javascript Helper function in a Laravel Project

I can Get base url of my laravel project from blade file in javascript section by using this

var APP_URL = {!! json_encode(url('/')) !!}

But what should i do to get the base url when i am in a helpers.js like file

isValid(token){
        const payload = this.payload(token);
        if(payload) {
            return payload.iss == "http://127.0.0.1:8000/" || "http://127.0.0.1:8000/register" ? true : false
        }
        return false
    }

i want to get base url here rather asssigning the url separately. Can anyone Help Me?

Use this to get base url in javascript.

  var base_url = window.location.origin;

Check it

var getUrl = window.location;
var baseUrl = getUrl .protocol + "//" + getUrl.host + "/" + getUrl.pathname.split('/')[1];

You Can Use like this:

var baseUrl = window.location.origin;
// "http://google.com"

var host = window.location.host;
// google.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM