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

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>

Related

Only js file per page in laravel

In a Laravel setup using Laravel mix, the mix.js(['resources/js/app.js','resources/js/new-offer.js'], 'public/js') puts everything in one file, app.js.
What I am trying to achieve is to have multiple .js files each for one page. For example. I want to have index.js for my index.blade.php and new-offer.js for my new-offer.blade.php etc ...
The idea is that each page is server side rendered and each page represents a static page comming fromm the server and each one should has it's own set of js files that works in. How this should be done, is it recommended, what are other arhcitectures ?
You can use multiple concatenated .js(...) statements. In your case:
mix.js('resources/js/app.js','public/js').js('resources/js/new-offer.js' ,'public/js')
This will createapp.js and new-offer.js files in your public js folder

Loop to load all existing HTML files into one content

How can I load all html files in a folder into a content? I have about 125 html files in a folder src and need to load them in a content called .content.
$(".content").load("src/1.html");
is this doable on client side using JavaScript?
I belive you need to use PHP to get the numbers of files on a specific folder on your root directory.
http://php.net/manual/en/function.scandir.php
If you read this, you will be able to get the folder array by using PHP and they you can use JS or JQuery to do rest.

CodeIgniter : Included JS or CSS in view folder

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);

Load a file with javascript externally using php

I have one file named ad.php. In this file i load some ads from external pages.
(ad.php):
<script type='text/javascript' src='www.example.org'></script>...
And one file named: (index.php).
I want to load the contents of the file ad.php finished and then spend it in the index.php file.
I don't want, that the user loads the javascript files.
i tried some ways:
file_get_contents
curl
But the javascript files were loaded in the index.php by the user.
Is it possible to load the ad.php externally using php?
If you change your php.ini and set
allow_url_include = 1
allow_url_fopen = 1
you can use include("http://example.com/ad.php")
this would be the best way if you don't want to use JS.
If you want to use JS you could use XMLHttpRequests

How to get the compressed file name in Drupal?

After compressing the JS file in Drupal I got a randomly JS file name.
My question how I can get this file name to add it to my javascript?
Drupal code: import and compress
<?php drupal_add_js(drupal_get_path('theme', 'mytheme') . 'mybigfile.js'); ?>
Random file name Result:
js_2KQpJNjjupC1Beoweoqpok2JCXhe0sJ6YBllaRDUXYQ.js
Trying to Call the mybigfile.js file:
<script>
telInput.intlTelInput({
validationScript: "mybigfile.js"
});
</script>
As far as I know, you can not get the name of the concatenated JavaScript file generated by Drupal using PHP.
Your best option would be to avoid your JavaScript file mybigfile.js from being concatenated and compressed. Then you can apply your validation script by calling the file name.
You will have to set the preprocess property to false inside drupal_add_js $options array, to prevent the file from being aggregated.
drupal_add_js(drupal_get_path('theme', 'mytheme') . '/mybigfile.js', array(
'type' => 'file',
'preprocess'=> false,
));
Update
If you want to compress a single JavaScript file, you have the option to use AdvAgg API.
Unfortunately, I can't find any documentation for how to do that. But you start with the following:
download and install advagg and enable the submodule advagg_js_compress.
inside the advagg_js_compress submodule folder, find the file advagg_js_compress.advagg.inc and open in your text editor. Here you can find a few helper functions that will help you compressing your JavaScript file.

Categories

Resources