<script src="<?php echo site_url('bootstrapper/jqTimer'); ?>"></script>
So I've tried to call the controller below using the script above but I get error not found. Any ideas as to why this method is not working?
function jqTimer() {
$this->load->view("jquery.timers.js");
}
When loading javascript with a <script> tag, the src attribute is expecting a file name to a js file. You're giving it a path to a controller method in your CI install.
What you need to do is put the jquery.timers.js file in the public_html folder and access it from there:
// assuming you have the script in a [javascripts] folder inside [public_html]
<script src="<?php echo site_url('javascripts/jquery.timers.js'); ?>"></script>
If you'd prefer to load your javascript through views, then you need to do this instead:
<script><?php echo $this->load->view("jquery.timers.js", "", TRUE); ?></script>
This will echo out the contents of the view file between the <script> tags for embedded javascript. (passing TRUE as the third parameter returns the content of the file, so you can echo it out)
I don't see any reason why you should create a function in the controller just to load a javascript file.
The base_url() function can already do what you want.
<script src="<?php echo base_url('path/to/jquery.timer.js'); ?>"></script>
Have a look at the documentation for the URL helper
You're looking to echo out a link to the js, not echo out the js itself.
You could create a MY_Controller in the core folder which extends CI_Controller, in which:
function jqTimer() {
return site_url("jquery.timers.js");
}
Then, in any other controller (which extends MY_controller) you'd go:
$data['js_timer'] = $this->jqTimer();
/* proceed to load main view with $data */
View:
<script src="<?php echo $js_timer; ?>"></script>
This is DRY in that, should you decide to use a different js timer plugin, you need to replace it in one place only: the MY_Controller class (as opposed to every view file).
Still, a bit of an odd way of doing things...
Related
I am trying to load a view from a controller file. If I reference my class in the view everything is fine, however if I reference a method from that class my css does not load.
Here is a simple example:
Controller file:
<?php
class Example extends CI_Controller
{
public function index ()
{
$this->load->view('myview');
}
}
In the view file this works fine:
<li>EXAMPLE</li>
But, this does not:
<li>EXAMPLE</li>
I also tried to use the site_url() function but the result was the same.
Note: I have loaded the URL helper.
Have you change your .htaccess file to remove index.php in url? then
<li>EXAMPLE</li>
it will not work.
you can try to add
<base href="<?php echo cdn_url()?>" />
into the tag <head></head>
Make sure that you have configured your config to php file properly. Then See take a look and make sure that have you removed index.php from url? if yes then remove index from every url you are targeting.I Hope it will work.
In config.php
$config['base_url'] = 'http://localhost/{your_project_folder}/';
I'm having trouble with the path when a file calls a css or a js file.
I'm using Codeigniter also Smarty template engine. My base URL is> http://local.project
My css and js files are located in> project/site/assets/css and project/site/assets/js
The thing is that when I call the resources from the view the path is a mess and not found error appears.
I have tried many ways but still can't make it. I begin to think that this can be a path problem from Codeigniter. I mean.. maybe I'm missing something that I can't figure what is.
When I do an inspection with the browser I can see that this is set by default>
http://local.project/
and then from the view I call it like>
<script type="text/javascript" src="{site_url()}assets/js/jquery.flot/jquery.flot.js"></script>
but the path that is built is>
http://local.project/%7Bsite_url()%7Dassets/js/jquery.flot/jquery.flot.js 400 (Bad Request)
I'm driving crazy here, any help would be very appreciated!
try:
<script type="text/javascript" src="/assets/js/jquery.flot/jquery.flot.js"></script>
or
<script type="text/javascript" src="/js/jquery.flot/jquery.flot.js"></script>
can you provide your ip,to view the page?
Try using base_url() instead of site_url().
try:
<?php
echo "<script type='text/javascript' src='" . base_url() . "assets/js/jquery.flot/jquery.flot.js'></script>" ;
?>
To use the base_url() you need to call in your controller the URL helper:
$this->load->helper('url');
Then in your view:
<script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery.flot/jquery.flot.js"></script>
First of all load the url helper
$this->load->helper('url');
secondly get base address using this
echo base_url();
and finally your base address is "http://local.project", no slash at the end, so try this
<script type="text/javascript" src="<?php echo base_url();?>/assets/js/jquery.flot/jquery.flot.js"></script>
and make sure your base address do not have any extra characters, like space or enter
I could make it. Just opened my Smarty.php file and changed this line view/admin just to views
$this->setTemplateDir(APPPATH.'views');
After that the url I could use from the view is
<script type="text/javascript" src="assets/js/jquery.flot/jquery.flot.js"></script>
I was trying to call "base_url()" inside of an external .js file.
my 'test.js' file path is "js/test.js".
my test.js code was:
function download(s){
var w=window.open('<?php base_url(); ?>home/download?f='+s+'.ogg','Download','height=100,width=100');
}
And I linked that file(my js file is working fine when trying simple JavaScript codes) :
<script src="<?php echo base_url(); ?>js/test.js" type="text/javascript"></script>
But that always said that the access was denied and asked for server permission.
I tried 'site_url()' too, even I tried just an echo "hello" in 'download' function but those didn't work. But when I add that code inside of header.php view file like:
<script type="text/javascript">
function download(s){
var w=window.open('<?php base_url(); ?>home/download?f='+s+'.ogg','Download','height=100,width=100');
}
</script>
Then that worked.
And in case of external CSS when I write:
background:url('<?php base_url(); ?>images/side.png');
That doesn't work. But if I write:
background:url('../images/side.png');
Then that works.
So how can I call "base_url()" inside of external .js file and .css file ?
You can't call base_url() in a javascript / css file.
You need to put a relative path in these js/css files.
PHP scripts aren't aren't run unless they're in a .php file.
For Using This Solution Just Keep in mind that you have to put the Script tag before the external js script.
<script type="text/javascript">
var baseURL = "<?php echo base_url(); ?>";
</script>
Now Use baseURL variable in that external js.
I am a newbie in codeigniter. I want to load jquery on every page. I know we have to use autoload.php. But inside it there is section where static contents like js or css can be included. How to do this? I created another index like this
$autoload['static'] = array(JS_LIBS_PATH.'/jq.js');
But obviously nothing happened. That constant has been defined in config,php. Using same constant I could put jq on a page, but what about autoload ?
I can think of several elaborate solutions but I really think that javascript is presentation-level and shouldn't be in your autoload, models, controllers, etc.
You can just use a master view file of some kind with your <head> and basic HTML requirements:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
<script src="/path/to/jquery.js"></script> <!-- jQuery on every page -->
<!-- alternatively, loop through files in a custom config setting -->
<?php foreach (config_item('default_js') as $src): ?>
<script src="<?php echo $src; ?>"></script
<?php endforeach; ?>
<!-- ... and/or loop through files set in a controller -->
<?php foreach ($js_files as $src): ?>
<script src="<?php echo $src; ?>"></script
<?php endforeach; ?>
</head>
<body>
<header />
<?php echo $content; ?>
<footer />
</body>
</html>
Then just load views like this:
$data['js_files'] = array('draggable.js', 'widgets.js');
$content = $this->load->view('index', $data, TRUE);
$this->load->view('master', array('title' => 'Home', 'content' => $content));
Of course this is just one solution out of millions, but the idea of Codeigniter "autoloading" CSS and Javascript makes no sense, you have to "load" it yourself in one way or another - how you do it is completely up to you.
Here's what I've done for a while that works quite well.
Your asset files (javascripts, stylesheets, etc) go in your asset folders /assets/js, /assets/css and so forth.
In your config file, you define a default array of JavaScript files to load.
$config['default_asset_js'] = array('js/jquery.js', 'js/jquery-ui.js');
In your views, you have a section that runs through that array and outputs each of them.
foreach($this->config-item('default_asset_js') as $file_name)
{
echo '<script src="' . base_url('assets/' . $file_name) . '"></script>';
}
You can probably also get creative and add to that array in your controller, if you have specific pages that require additional files to be loaded.
In CI, Helpers, as the name suggests, help you with tasks. So we can load jquery automatically as a helper.
Put the jquery file in some place: /var/www/html/ci3/assets/js/jquery-2.1.4.min.js
Create a file /var/www/html/ci3/application/helpers/jquery_helper.php (you need use the _helper.php sufix).
Inside the jquery_helper.php file call the jquery-2.1.4.min.js file:
<script src="assets/js/jquery-2.1.4.min.js"></script>
Inside /var/www/html/ci3/application/config/autoload.php call the jquery_helper.php:
$autoload['helper'] = array('jquery');
Now the jquery will be load automatically and you can use it in all your views.
As title said, I'm trying to figure out how to use javascript and jquery libraries on CI.
Following instruction in the docs, I load the library in my controller:
$this->load->library('javascript');
Then, I define the location of the jQuery file (jquery.min.js) in the config.php:
$config['javascript_location'] = 'http://localhost/ci/assets/js/jquery/');
After that, I open the view file and put in these two lines:
<?php echo $library_src;?>
<?php echo $script_head;?>
First error comes up here: Undefined variable $library_src and $script_head (don't understand where I have to set them)
Anyway, I've commented these lines and continue with jquery lib, by loading it in my controller with:
$this->load->library('jquery');
Next error: Unable to load the requested class: jquery. (it seems that it can't find the lib, what i messed up?)
Checking on system folder it looks all files are in place:
system/libraries/Javascript.php
system/libraries/javascript/Jquery.php
Thanks in advance for your help!
Put the code in the config.php like this:
$config['javascript_location'] = 'js/jquery/jquery.js';
$config['javascript_ajax_img'] = 'images/ajax-loader.gif';
In your controller file (e.g. controllers/sample.php) type this codes:
function __construct()
{
parent::__construct();
$this->load->library('javascript');
}
function index()
{
$data['library_src'] = $this->jquery->script();
$data['script_head'] = $this->jquery->_compile();
$this->load->view('sampleview', $data);
}
In your view file (e.g. views/sampleview.php) type this codes:
<?php echo $library_src;?>
<?php echo $script_head;?>
This works for me. I hope it works for you too. XD
It is important to note that this Driver is marked as experimental so I wouldn't rely on it.
Also, personally I think it's asking for confusion and headaches to try and directly mix server-side portions of your applications with client side portions.
To use javascript in your views, I would just start out by loading them like this...
<script type="text/javascript" src="<?= base_url() ?>path/to/jquery.js"></script>
Because this driver is experimental the documentation isn't quite there yet. But I was able to find a solution.
First, the documentation has an error in it. Unless you change the core Javascript library (not suggested) the reference variable is not $script_head but in fact$script_foot.
Second, once you've finished making your calls, it seems you need to run
$this->javascript->external();
and
$this->javascript->compile();
These functions set the $library_src and $script_foot variables.
To put it all together, in your controller you would have:
class Some_Controller extends CI_Controller {
public function index()
{
$this->javascript->click('#button', "alert('Hello!');");
$this->javascript->external();
$this->javascript->compile();
$this->load->view('index');
}
}
In your view you would have
<html>
<head>
<?php echo $library_src; ?>
<?php echo $script_foot; ?>
Although CI first looks for system folder, you can also try putting your libs in the these folders:
application/libraries/Javascript.php
application/libraries/javascript/Jquery.php
Not sure why you have to load your js file via controller and then echoing it in your view. You can just load ur js file using HTML tags directly in your view. Passing data from controller and echoing it in view is mostly used when your variable's value is dynamic/ needs to be loaded from databases etc.
As we know on the user guide, first, it was an experimental but working. Thr first step is open your config file under application/config/config.php .
Put the following line :
// path to JS directory you want to use, I recommended to put the .js file under 'root app/js/yourfile.js'
$config['javascript_location'] = 'js/yourfile.js';
Second step is open your controller, within constructor method, put the following code :
// Load javascript class
$this->load->library('javascript');
$this->load->library('javascript/jquery'); // if u want to use jquery
Then, still in controller file within index method :
$data['library_src'] = $this->jquery->script(); // Because I refer to use jquery I don't test to use $this->javascript->script().
Then open your view file and put the following code within tag head :
<?php echo $library_src; ?>
That way is working for me, let's try it.
Use:
$this->load->library('javascript/jquery');
instead of:
$this->load->library('jquery');
This will load your jQuery library.
I had the same problem, and found :
<?php $this->load->library('javascript'); ?>
<?php $this->load->library('javascript/jquery'); ?>
on https://ellislab.com/forums/viewthread/181742/#860506
Because jquery is in javascript folder.
Or
$autoload['libraries'] = array('database', 'session', 'javascript', 'javascript/jquery');
In the autoload.php file.
That solved the problem of loading the librairy.
You can try this, it's work to me.
in config.php
$config['javascript_location'] = 'http://localhost/ci/js/jquery.min.js';
in Controller
public function __construct() {
parent::__construct();
$this->load->library('javascript');
$this->load->library('javascript/jquery');
}
public function index(){
$d['library_src'] = $this->jquery->script();
$d['logalert']=$this->jquery->_show('#logalert',1000,"$('#logalert').html('hello world');");
$this->load->view('main',$d);
}
in view (head)
<?php echo $library_src; ?>
in view content (body)
<div class="alert alert-dismissable alert-info" id="logalert">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
</div>
in javascript view
<script type="javascript">
$(document).ready(function(){
<?php echo $logalert; ?>
};
</script>
happy coding :)