CodeIgniter : Included JS or CSS in view folder - javascript

I'm include JS or CSS in Folder views codeigniter but experienced a
403 error,
How to solve it?
example code :
<script type="text/javascript" src="<?php echo base_url() . APPPATH; ?>views/data/index.js">
or
<link type="text/css" rel="stylesheet" href="<?php echo base_url() . APPPATH; ?>views/data/index.css">
then came the error "
NetworkError: 403 Forbidden
Kindly solution of masters.
Sorry if my english language is not good

Easiest Answer to your problem is to create new directory in your application root like this:
-Your_Application
-application
-assets <-- Create this folder.
-css <-- Create this subfolder inside assets folder.
-js <-- Create this subfolder inside assets folder.
-script.js <-- Place your script files inside js folder.
-system
-index.php
Now you are able to access your css and js files using url helper like this:
<script src="<?php echo base_url('/assets/js/script.js'); ?>" ></script>
Follow same procedure for css and make sure url helper is enabled from config/autoload.php like this:
$autoload['helper'] = array('url');
Want to know the reason behind this:
The reason behind happening this is that your application outputs from index.php in your application root as CI implemented Front-controller design pattern.
So we don't need to place our public assets anywhere inside our application that's why CI restrict direct access to application and system folder from the public access.

In your view you cannot have assets or supporting files on it. That wrong file structure of the codeigniter.
You can have a folder name asssets on a root and follow this steps.
you can add url path on constraints.php on config folder .... you define the url like this on constants :
NOte: xxx is your site name
define('ROUTE_STIE_PATH','localhost/xxx/');
define('ADMIN_CSS_DIR_FULL_PATH',ROUTE_STIE_PATH.'assets/css/');
define('ADMIN_CSS_DIR_FULL_PATH',ROUTE_STIE_PATH.'assets/js/');
and then you can echo by simply on view or header
<link href="<?php echo ADMIN_CSS_DIR_FULL_PATH; ?>index.css" rel="stylesheet" type="text/css">
<script src="<?php echo ADMIN_JS_DIR_FULL_PATH; ?>index.js" type="text/javascript"></script>

CodeIgniter comes with a .htaccess in the /application directory which contains
Deny from all
You'll need to remove this file or change it's ruleset to access js/css files etc from anywhere inside
application/*
It is however largely inadvisable to allow access entirely to the application.

if its your requirement to have js or css file specific to view and you want to handle those file within view then:
Simplest way is, create file
xxxcss.php and xxxjs.php //put your js code or css style
Define Variable if required
$data['variable']="Something"; // This is optional if you need pass php variable to your js or css before loading in page.
load view in controller and pass to another view or directly load within view
$this->load->view('css.php or js.php',$data);

Related

Laravel edit page does not call CSS and JS when on a server

I made an edit page for my application which is accessible via the index. I have a master.blade.php file that contains all the references for scripting and the CSS links. and every page calls this master page to make use of the Nav bar, heading and the CSS/JS. The edit page does not seem to call this and on the console it shows 404 not found for these CSS and JS files when I pushed this project on a server. Does anyone know what I am doing wrong
I have changed the routing of the CSS/JS files to route correctly and that calls correctly everywhere else but this page.
Here is a sample of my paths:
<link href="../assets/css/bootstrap.min.css" rel="stylesheet" />
this is in my master page, so in my other pages I call:
#extends('layouts.master')
In Laravel, best way to import any asset, is using asset function in your blade file:
asset('path to css')
This is work for images, CSS and JS files, and other files to import into a blade or HTML.
just upload your files into "public" folder like this folders scaffolding:
---- public
---- ---- css
---- ---- ---- bootstrap.min.css
and use asset function like this:
<link href="{{asset('css/bootstrap.min.css')}}" rel="stylesheet" />
I hope this response being useful for you!
use asset function like this
<link href="{{asset('path')}}" rel="stylesheet" />

ASP.NET Web Forms Routing and paths to js files

I am working on an ASP.NET Web Forms project that make use of Routing.
I have two routing situations:
http://mywebsite.com/section
http://mywebsite.com/section/subsection
My js files are stored in a /js folder in the root of the project.
Fact is, when I load /section page everything works fine, the js are loaded correctly. When I load /section/subsection page the js are not loaded and actually the path appear to be something like
http://www.mywebsite.com/section/file.js
I have already tried to put a slash before the name of the js folder or even to use a server side ResolveUrl function like
<script src='<%=ResolveUrl("~/js/file.js")%>'></script>
In this case file.js il loaded correctly; anyway if file.js contains calls to an external js file, the latter is not loaded (its path is always in the form of /section/externalfile.js).
Adding the following line to the Global.asax
routes.Ignore("{*allfiles}", new { allfiles = #".*\.(css|js|gif|jpg|png)" });
did not solve the problem either.
The most surprising thing is the fact that, for instance, image or CSS files are loaded correctly even with a normal link like the following:
<link rel="stylesheet" type="text/css" href="css/owl.theme.css" media="all" />
Is there anyone who has an idea of how solving this issue ?
Edit: The master page of my project calls a cutom.js file (which is loaded correctly). This file contains for instance the following call to superfish.min.js:
Modernizr.load([{
load: kopa_variable.url.template_directory_uri + 'js/superfish.min.js',
complete: function() {
$('.top-menu').superfish({});
}
}]);

.js files not being called on pages included in folders

At the bottom of every page, I have a .php include that links to all my .js files.
<?php include 'Core/js.php';?>
Within this .php I have this code;
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.dropotron.min.js"></script>
<script src="../js/skel.min.js"></script>
<script src="../js/skel-layers.min.js"> </script>
<script src="../js/init.js"></script>
<script src="../js/slider.js"></script>
This works perfectly for my pages placed in my root folder, ie "index.php"
However, the pages that are located in folders, don't seem to call the javascript when I use the .php include such as;
<?php include '../../../Core/js.php';?>
Although, when I don't use the include funtion, and just paste the < script>, it calls it perfectly. This wouldn't be a huge problem for me, but it doesn't allow the mobile site to run properly.
The first pages such as "index.php" have the mobile navigation, whereas pages located in the folders and don't have the php include code, don't have the same user friendly navigation. If someone could help me fix this, that would be great!
I think your problem is about paths.
When you execute: <script src="../js/jquery.min.js"></script> in your browser, it looks at the URL and goes from there. Let's say you're in http://example.com/products/index.php. The browser will try to load the JS from http://example.com/products/../js/jquery.min.js, which is http://example.com/js/jquery.min.js.
To avoid this, you should use absolute paths, like:
<script src="/js/jquery.min.js"></script>
Then it will always try to load http://example.com/js/jquery.min.js independently from the current URL.
As for PHP includes, I would advise you to use absolute paths when including files. There are many strategies, like using $_SERVER['DOCUMENT_ROOT'], using dirname() functions, using a global variable with your includes path, etc.
Whatever you choose, your includes should look something like:
<?php include '/var/www/includes/Core/js.php'; ?>
I'm assuming your folder structure is something like this:
/
index.php
js
jquery.min.js
jquery.dropotron.min.js
skel.min.js
...
Core
js.php
content
some folder
HTML files
...
So if you are inside content -> some folder -> html file your js.php should reflect this:
<script src="../../js/jquery.min.js"></script>
as you could see the path changes and thats where your error comes from
Use the absolute path. Your absolute path is the actual location on the server. An easy way to find it is look at the path you're connecting to with ftp.
It might look something like this /home/username/public_html/Core/js.php

How to load a plugin image with a plugin javascript script in CakePHP?

I have a (javascript) script in my CakePHP plugin which creates an img tag in the current viewed document. The problem is, that every try to provide a valid image source have failed until now. The plugin resides in the plugin directory of the CakePHP library (not the CakePHP app), as we use it for several independent apps which have a lot of commons.
The images are located in /cake/plugins/Corporate/webroot/img.
The (javascript) script is located in /cake/plugins/Corporate/webroot/js.
The relative path from script to image does not work (../img/image.png). The CakePHP routing path (/Corporate/img/image.png) does not work either, independent if I prepend the plugin name or not.
Use the right path
The right path to get to a webroot asset in the PluginName plugin is of the form:
/plugin_name/path
I.e.:
Filepath: App/Plugin/Corporate/webroot/img/image.png
Url: /corporate/img/image.png
In early versions of CakePHP asset dispatching was automatic, while in 2.2+ asset dispatching must be explicitly enabled.
The normal way to deal with plugin assets is to not use the asset dispatch filter and to copy (or symlink) the files to the application webroot in the same path as corresponds to the url i.e.:
cd App
cp -R Plugin/Corporate/webroot webroot/corporate
This means that the url requested directly maps to a file in the application webroot folder - there is no php logic invoked in serving these files which amongst other benefits makes serving such assets significantly faster.
Dealing with paths in javascript
If an application is always, always installed as the root of the domain (http://example.com) there is absolutely no complexity - just refer to the relative path to the image:
var imagePath = '/corporate/img/image.png';
If however the root of the application is variable (http://example.com/, http://exmaple.com/sub/folder/, http://localhost/someproject/) - it's necessary to tell the javascript what to consider the root of the application. for example put the following in the layout file:
<head>
...
<script>ROOT = '<?= $this->Html->url('/'); ?>';</script>
</head>
<body>
<?= $this->fetch('content'); ?>
...
</body>
In this way, javascript can refer to the global ROOT anywhere to know what the root url is, like so:
var imagePath = ROOT . 'corporate/img/image.png';
This makes it possible to use the same javascript irrespective of how the application is installed.
you can use this code
<?php echo $this->Html->image("recipes/6.jpg", array( "alt" => "Brownies", 'url' => array('controller' => 'recipes', 'action' => 'view', 6))); ?>
Altough, the question is very old but still I want to answer this for anybody else looking for it
You could use
<?php
echo $this->Html->image( $this->Html->url('/', true).'{plugin_name}/{folder_name}/{asset_name}');
?>
For example,
<?php
echo $this->Html->image( $this->Html->url('/', true).'Corporate/img/image.png');
?>
Note here that plugin_name and asset_name are case sensitive.

How to add and use an external js file in Codeigniter?

I am trying to add an external javascript file to Codeigniter application.
I have placed my external js file in the "projectname/js/" folder.
Below is the script tag src in my view page.
src="http://localhost/needpcbcodeigniter/js/registration.js">
But I am getting a "Not found" error for the js file.
Most probably this is because the apache mod_rewrite is dealing with the js folder as a codeigniter controller name (as if you're calling http://localhost/projectname/index.php/js/registration.js)
Check your .htaccess to make sure it doesn't redirect when a valid filename is requested. Check it here
You should add an external javascript file to codeigniter in assets folder.
You should check some following steps :-
1) first fix your project base_url which is set in application/config/config.php like-
$config['base_url'] = 'http://localhost/yourprojectname/'
2) create a folder(e.g: js) in assets folder(at root of folder) for save all javascript files(e.g : registration.js).
3) on final step where you want to use javascript file use code like this-
<script src="<?php echo base_url();?>/assets/js/registration.js"></script>

Categories

Resources