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>
Related
I routed my controller function and suddenly the function name is added to base URL, how can I remove this?
$route['editblog/(:num)']='Code/editblog/$1';
Then all of the sudden edit blogs are added to base URL and my external files are showing an error.
<script src="assests/vendor/slick/slick.min.js">
It shows:
<script src="editblog/assests/vendor/slick/slick.min.js">
Please help me, help will be highly appreciated!
You need to verify 1 things:
application/config.php $config['base_url'] = 'application/path/'; like http://example.com/ or http://localhost/applicationName/
Now you should use urls like
base_url('assets/js/filename/js');
base_url('controller/action/parameters');
In this way, it will generate correct urls.
You should change your URL's to:
<script src="<?php echo base_url('assests/vendor/slick/slick.min.js'); ?>">
Actually base_url is set in your applications/config.php file. -> config['base_url']. It MUST include trailing slash
in your config file:
$config['base_url'] = 'http://test.com';
when you call out the base_url(). You can use this :
<script src="<?= base_url().'/assests/vendor/slick/slick.min.js'; ?>">
I would like to reference JavaScript files from my root folder in a WordPress PHP file, however when I load the page, the JavaScript is being ignored. Is there a WordPress restriction on referencing .js files? Am I not correctly referencing JavaScript for PHP?
This is what I wrote (which does not work):
<?php wp_footer(); ?>
<script src="/files/js/jquery.js" type="text/javascript"></script>
<script src="/files/js/jquery.cycle.js" type="text/javascript"></script>
</body>
</html>
For jQuery I use this technique Loading The Latest Version of jQuery in WordPress
Basically you use wp_enqueue_script function in functions.php.
For scripts where I want to simply add them in the template files and don't want to bother with wp_enqueue_script, I put them in scripts folder that was created in the theme folder. I add All custom added .js scripts there.
Then in the template file I use the code like this:
<script type="text/javascript" src="<?=get_template_directory_uri();?>/scripts/markers.js"></script>
Wordpress has its own method to add js or css files.
<?php
function my_scripts_loader() {
wp_enqueue_script( 'my-js', 'filename.js', false );
}
add_action( 'wp_enqueue_scripts', 'my_scripts_loader' );
?>
Put your files folder to your wordpress theme which you are curently useing after that just need to
include the path on header.php file like
<script src="<?php bloginfo('template_directory'); ?>/files/js/jquery-min.js" type="text/javascript"></script>
hope this will help you ....
WordPress got a various number of ways to include file, I myself have worked with WordPress in the early days, back then I had to call the path with a WordPress function.
Example: <script src="<?php echo bloginfo('template_directory'); ?>/files/js/jquery.js" type="text/javascript"></script> //add echo
You might want to var_dump the bloginfo('template_directory') and work your way from there.
I want use a jQuery plugin in category.tpl. Put files in javascript/jquery directory. Now, how can use this plugin?
/* one can load JS like that: */
if(file_exists('catalog/view/javascript/'.$this->config->get('config_template').'/script.js')) {
$this->document->addScript('catalog/view/javascript/'.$this->config->get('config_template').'/script.js');
}
It is rather "the proper way" to use existing functions, than to add scripts manually into header.tpl.
As a hint, based upon the answer below - one could loop an array of filenames, in order to keep control over the loading order, which is often relevant while they might depend on each other.
I've never used OpenCart, but a quick google session tells me that you should include the plugin scripts (just like any other js) in a file called header.tpl.
Here is a part of an sample header.tpl-file I found:
<script type="text/javascript" src="catalog/view/javascript/jquery/jquery-1.3.2.min.js"></script>
Just add a the following line below the jQuery include so it looks like this:
<script type="text/javascript" src="catalog/view/javascript/jquery/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="catalog/view/javascript/jquery/[PLUGIN FILE NAME].js"></script>
and you should be good to go.
First paste your jquery files, css files and images in catalog/view/javascript/yourplugin folder.
Then call the jquery plug in files in catalog/view/theme/yourtheme(default)/template/product/category.tpl file.
For ex,
YOur php code;..
...
....
<script src="catalog/view/javascript/jquery/jquery-ui-min.js"></script>
<script type="text/javascript" src="catalog/view/javascript/jquery/jquery.anythingslider.js"></script>
<link rel="stylesheet" href="catalog/view/theme/default/stylesheet/anythingslider.css">
<script>
// DOM Ready
$(function(){
$('#slider').anythingSlider();
$('#slider1').anythingSlider();
$('#slider2').anythingSlider();
});
</script>
its for slider.. you can do your action in php (above the script).
You'll need to include JS and CSS sources in Header View (/catalog/view/theme/[your theme]/template/common/header.tpl)
in config.php
define('DIR_JAVASCRIPT', 'D:\wamp\www\opencart/view/javascript/your_dir/');
in header.tpl
<?php
if (is_dir(DIR_JAVASCRIPT)):
if ($handle = opendir(DIR_JAVASCRIPT)):
while (false !== ($file = readdir($handle))):
if (preg_match('#\.js$#', $file)):
?>
<script type="text/javascript" src="<?php echo 'view/javascript/your_dir/'.$file; ?>"></script>
<?php
endif;
endwhile;
closedir($handle);
endif;
endif;
?>
<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...
I would love to post some starting code in this question, but to be honest, I' stumped.
I'm looking for a way to use PHP within my .JS file.
I would like to be able to use real PHP functions inside my "stuff.js" and then be able to include it using:
<script type="text/javascript" src="whatever.js"></script>
Any thoughts? Thanks.
<script src="/YOUR_PHP_FILE_PATH/file.php"></script>
Also add header in php file
header(Content-Type: text/javascript);
You just have to write your JavaScript into a php file and then include it
<script type="text/javascript" src="whatever.php"></script>
I would recommend you keeping your JS functions in a .js file and the variable data that needs to be output dynamically could be output directly in the main php/html file:
<script type="text/javascript" language="JavaScript">
var a = "<?php echo addslashes($a); ?>";
// ...
</script>
Other possibility would be to tell your server to proccess *.js file with php but I wouldn't recommend that as it could cause some unexpected problems.
It's impossible but you can use PHP file as a JavaScript file talking in a short way. ;)
<script type="text/javascript" src="whatever.php"></script>
And in whatever.php:
<?php
echo "var text = 'test';";
?>
alert(text);