I am building a website with CodeIgniter, I have various resources that I load with the base_url helper function like this
I may be late but for all the Future geeks. Firstly i suppose you want to call base_url in your .js
file. so lets consider you are calling it on below sample .js
file
sample.js
var str = $(this).serialize();
jQuery.ajax({
type: "POST",
url: base_url + "index.php/sample_controller",
dataType: 'json',
data: str,
success: function(result) {
alert("Success");
}
In the above there is no base_url assigned.Therefore the code wont be working properly. But it is for sure that you'll be calling the .js
in between <head></head>
or <body></body>
of View file by using <script> </script>
tag. So to call base_url
in.js
file, we have to write the below code where you plan to call the .js
file. And it is recommended that you create common header file and place the below code and all the calling style sheets (.css
) and javascript (.js
) file there. just like below example.
common header file
<head>
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/js/sample.js">
<script>
var base_url = '<?php echo base_url(); ?>';
</script>
</head>
Now the base_url
will work in sample.js
file as well. Hope it helped.
Should you want what is exactly specified in the web page, just use:
document.querySelector('head base')['href']
You can access the current url quite easily in JavaScript with window.location
You have access to the segments of that URL via this locations
object. For example:
// This article:
// https://stackoverflow.com/questions/21246818/how-to-get-the-base-url-in-javascript
var base_url = window.location.origin;
// "http://stackoverflow.com"
var host = window.location.host;
// stackoverflow.com
var pathArray = window.location.pathname.split( '/' );
// ["", "questions", "21246818", "how-to-get-the-base-url-in-javascript"]
In Chrome Dev Tools, you can simply enter window.location
in your console and it will return all of the available properties.
Further reading is available on this Stack Overflow thread
You can make PHP and JavaScript work together by generating the following line in each page template:
<script>
document.mybaseurl='<?php echo base_url('assets/css/themes/default.css');?>';
</script>
Then you can refer to document.mybaseurl anywhere in your JavaScript. This saves you some debugging and complexity because this variable is always consistent with the PHP calculation.
in resources/views/layouts/app.blade.php file
<script type="text/javascript">
var baseUrl = '<?=url('');?>';
</script>
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;
}