php


how to add script in controller in CI


I have a admin.js and admin.css file. I have to include it inside admin.php that is a controller. because that would be better to include that js and css in perticular module instead of adding it in header.php. Thanks in advance.
As far as I know, there is no built in CodeIginiter function to include this output like there is for CSS using the link_tag() function provided by CI. I've added a function called script_tag() to the system/helpers/html_helper.php file from CI. The function is:
if ( ! function_exists('script_tag')) {
function script_tag($src = '', $language = 'javascript', $type = 'text/javascript', $index_page = FALSE)
{
$CI =& get_instance();
$script = '<scr'.'ipt';
if (is_array($src)) {
foreach ($src as $k=>$v) {
if ($k == 'src' AND strpos($v, '://') === FALSE) {
if ($index_page === TRUE) {
$script .= ' src="'.$CI->config->site_url($v).'"';
}
else {
$script .= ' src="'.$CI->config->slash_item('base_url').$v.'"';
}
}
else {
$script .= "$k=\"$v\"";
}
}
$script .= "></scr"."ipt>\n";
}
else {
if ( strpos($src, '://') !== FALSE) {
$script .= ' src="'.$src.'" ';
}
elseif ($index_page === TRUE) {
$script .= ' src="'.$CI->config->site_url($src).'" ';
}
else {
$script .= ' src="'.$CI->config->slash_item('base_url').$src.'" ';
}
$script .= 'language="'.$language.'" type="'.$type.'"';
$script .= ' /></scr'.'ipt>'."\n";
}
return $script;
}
}
Then in your PHP code you can do:
echo script_tag('content/js/jquery-1.4.2.js');
You're asking about how to include js/css just like what the link tag does?
class Admin extends CI_Controller {
function __construct(){
parent::__construct();
}
public function index(){
$this->template->add_css('path_to_css/admin.css',TRUE);
$this->template->add_js('path_to_js/admin.js',TRUE);
}
}
Instead of including script file in controller you can include script file in view page.
For example :
View page : Test.php
<script type="text/javascript" src="../application/js/validator.inc.js"></script>
<div>
//your code
</div>
Hope this will help you... :)

Related Links

WooCommerce reference hooked template for sku wrapper
Embedded PHP in login.php file returning HTTP ERROR 500 [duplicate]
Post not getting submitted in case I put ' [duplicate]
PHP 7 mysqli_real_escape_string() Error [duplicate]
How to export MySql Database to Excell where columns is format as text
It fetches data when i search integer values but not character [duplicate]
converting mysql data to json url
unable to handle request error 500 php email
Allowing only the clients i want to send requests
htaccess is not redirecting me on index.php page or default page
PhpStorm: Notes Plugin
Codeigniter get id of user and pass to form
Make array of $row
How to retrieve multiple user's recent videos using the YouTube API v3
mysql match against values from a table
PHP - Delete from multi-dimensional array by value [closed]

Categories

HOME
cocoapods
google-chrome
macos-sierra
lambda
applescript
concurrency
writefile
openlayers-3
magento-2.0
tabs
moodle-api
tfs2015
ag-grid
mithril.js
angular2-template
mailing-list
azure-graph-api
bootstrap-switch
vmware-workstation
reselect
nest-api
uima
jitsi
anova
function-points
dragula
echosign
acrobat
spring-annotations
ninject
unmarshalling
social-networking
tilemill
dotcms
quick-nimble
winrm
grails-2.5
nppexec
runtimeexception
jade4j
identify
aurelia-cli
cloudera-quickstart-vm
telephonymanager
jqgrid-asp.net
gcal
image-registration
gtk#
sesame
apache-modules
msxml
jpda
seccomp
xcode8-beta4
oci
jtag
groovyfx
eclemma
mercurial-extension
teamviewer
openmrs
teensy
godeps
unhandled
scala-macros
mathematical-expressions
subview
dc
ildasm
qt5.4
igor
aho-corasick
git-reset
spring-social-linkedin
profiles
postgres-xc
objective-c-runtime
indesign-server
html-frames
jquery-cycle
itunes-sdk
firebird1.5
liveconnect
fitch-proofs
infopath-2007
filelock
data-dump
pyjamas
suphp
activestate
yahoo-maps
3270
projectpier
.net-client-profile
main-method
accumulator
levels
regioninfo
remote-working
html-generation

Resources

Encrypt Message