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?
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With