Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get base_url using Jquery in Laravel

I am using laravel 5.0. I want to get base url of laravel page using jquery. I have tried the following function.

function getBaseURL () {
   return location.protocol + "//" + location.hostname + (location.port && ":" + location.port) + "/";
}

But this gives only http://localhost. I need my full base url in jquery. What to do?

like image 610
Praveen Srinivasan Avatar asked Jun 19 '15 12:06

Praveen Srinivasan


People also ask

How to Get Base URL in Laravel?

To access a named route, you could use the following: echo route('posts'); This would check your routes file and return the correct route with name posts , e.g. https://yourdomain.com/posts . Alternatively to the url() helper, you could use the URL facade.


1 Answers

Do you mean you want the base url of your laravel app available anywhere your javascript?

You can include this in your template.blade.php:

<script type="text/javascript">
var APP_URL = {!! json_encode(url('/')) !!}
</script>

And than access that var anywhere in your javascript:

console.log(APP_URL);
like image 145
Björn Avatar answered Oct 20 '22 00:10

Björn