I am including a script file using the following line:
<script src="<?php echo FCPATH;?>includes/js/script_admin_index.js"></script>
But the error I am getting is:
GET http://localhost/var/www/treed/treed_admin/includes/js/script_admin_index.js (404 NOT FOUND)
instead of including the files.
I dont want to use base_url as user will be able to navigate and see the file through the browser as well.
You need to use the base_url() to include the javascript file in your VIEW.
For ex.
<script type="text/javascript" src="<?php echo base_url();?>js/jquery-ui.min.js" ></script>
For CSS you can use link_tag.
For ex: <?php echo link_tag('css/AdminLTE.css'); ?>
You will need the URL helper and html helper.
$this->load->helper('url');
$this->load->helper('html');
You can block direct all to js and css file.Read this
Related
I have written a PHP page template and would like to link the needed Javascript from an external file. However, when I do this I get a syntax error: Uncaught SyntaxError: Unexpected token < referencing the doctype header <!DOCTYPE html>. If I put the script in the PHP file directly then it works.
Obviously this is making my file messy, so how can I do it without breaking the script? Also, since this script is only needed on this one page I don't want to put it in my footer to load on the whole site.
I have tried to use <script type="text/javascript" src="staff-test.js"></script> in my PHP file to reference the JS file, with no success. The PHP and the JS files are in the same folder, so the src path seems to be correct.
The PHP:
<?php
get_header();
include('staff-test.css');
?>
<div class="page-wrap">
// my content
</div>
<script type="text/javascript" src="staff-test.js"></script>
<?php
get_footer();
Using this code I get a syntax error. I would like to keep the JS in a separate file, but seems to only work when I put it directly in the PHP file.
Using include('staff-test.css'), you are asking your web server to interpret the CSS file as PHP. This is why you are getting a syntax error.
You want to do instead:
<link rel="stylesheet" type="text/css" href="staff-test.css">
Try this
<?php
get_header();
?>
<link rel="stylesheet" type="text/css" href="<?php echo get_stylesheet_directory_uri(); ?>/staff-test.css">
<div class="page-wrap">
// my content
</div>
<script type="text/javascript" src="<?php echo get_stylesheet_directory_uri(); ?>/staff-test.js"></script>
<?php
get_footer();
?>
You may need to register the script in your functions file. Check out the WordPress reference for registering a script.
https://developer.wordpress.org/reference/functions/wp_register_script/
Normally that is how one includes JS files in HTML:
<script type="text/javascript" src="myFile.js" ></script>
But I want to write the JS file inline the HTML, using PHP; but still having the possibility of having my neat JS file, that can be chached, debugged and minified.
How can I do something like this?
<script>
<?php include 'myFile.js';?>
</script>
In other words, how to include a JS file in HTML on the server-side. That may get a lot of advantages because it is transparent to the browser.
How can I do something like this?
<script>
<?php include 'myFile.js';?>
</script>
Exactly that code would work, yes. Though it would try to interpret the file as PHP code, and if anything looked like <?php .. ?> inside that file you may see weird side effects. If you want to include the file uninterpreted, simply do:
<script><?php echo file_get_contents('myFile.js'); ?></script>
Be aware of possibly having to escape "</script>" now, should that appear in your included file; see https://stackoverflow.com/a/6908901/476.
YOu need to make custom function or use :
echo '<script type="text/javascript" src="myFile.js" ></script>';
Or
function add_js($filename){
return '<script type="text/javascript" src="'.$filename.'" ></script>';
}
echo add_js('myFile.js');
I have a CI folder structure like this:
-> root
- - >application
- - >resources
- - >system
In the resources folder, I have separate folders like 'css', 'images', 'js', 'js/plugins' etc.
I have a twitter plugin in js/plugins folder which needs the base path of the site. I used it like this :
modpath: 'resources/plugins/twitter/',
And it works if the url that i visited is localhost/myapp/ , but doesn't work when the url is localhost/myapp/index.php/welcome
What i have to do ?
Because it is not a controller or a view, i can't use it as <?=site_url();?> right? So what i need to do?
Thanks for help !
You should use the full path to your files, in my CI projects I use something like this:
<script src="<?=base_url()?>js/twitter.js
Remember that your assets must be in the /public folder.
In your view file, before loading any javascripts add something like this
<script>
var base_url = '<?php echo base_url(); ?>';
</script>
This would make a javascript variable called base_url available to all other files you load after the definition.
Now in your javascript file you can do something like:
modpath: base_url + 'resources/plugins/twitter/',
Assume your base_url() is localhost/myapp/
Then use $this->config->base_url() in src like
<script src="<?= $this->config->base_url(); ?>resources/plugins/twitter/twitter.js"></script>
Check this with ur view file
<script src="<?php echo base_url();?>resources/plugins/twitter/twitter.js"></script>
I usually do this, in my View:
<script src="<?php echo base_url();?>resources/js/yourscript.js"></script>
<link rel="stylesheet" href="<?php echo base_url();?>"resources/css/bootstrap.css" />
to load resources (script files, or anything else) that stored locally.
Create js folder on the root where application and system folders are placed then place javascript file on that folder then on the view where you want to use this javascript file write down the following path:-
<script src="<?php echo $this->config->item('base_url') ?>js/fileName.js"></script>
I am using php web framework codeignter, I would want to access my link on Javascript, I've tried
<script type="text/javascript" src="<?php echo "assets/main.js;?>"></script>.
Autoload url helper is enabled but it doesn't load my Javascript.
Thanks!
simple and clean way is here:
create a helper class:
create a utility_helper.php class in helper folder and only put the below code in the utility_helper.php
code to put: (with <?php ...below code here... ?>)
function asset_url(){
return base_url().'assets/';
}
open your autoload.php and make sure you include utility in autoload['helper'] array:
$autoload['helper'] = array('url','utility','file','form','etc...');
important:
now create assets folder at the same location where you have your system and application
folders.. and put your all css and js files and folder in assets folder
now you can link your css and js in view like below:
<script src="<?php echo asset_url(); ?>js/jquery.js"></script>
<link href="<?php echo asset_url(); ?>css/style.css"/>
<link rel="icon" href="<?php echo asset_url(); ?>img/favicon.ico" type="image/x-icon"/>
GOOD LUCK :)
Non-programmer here, trying to figure something out.
I have a javascript function in the header of my document that upon page load it opens another page in an iframe and reveals an svg file on my server for minor online editing. I would like to place my javascript function in a php file, so that these folder locations of the svg files cannot be determined for anyone to download these svg files freely.
Currently I have this on my html header:
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
Since I have heard that php is a server side script and not viewable, I thought this would mask the location of these files on my server.
I want to remove this javascript code from my header and place it in a php file and replace the header code with either of these:
<script src="phpjavafile.php"></script>
or
<?php include ('phpjavafile.php'; ?>
and finally put the javascript into a php file like this:
<?php
<script type="text/javascript">
function loaded()
{
document.getElementById("myiframe").src="http://www.mydomain.com/myfolder/mypage.html?url=myotherfolder/"+window.location.search.substr(1);
}
onload=loaded;
</script>
?>
I have tried both of these methods and neither load properly.
I must be doing something wrong. Am I on the right track, or is there a better way of getting this to work.
Thank you ahead of time.
By using the echo() function
PHP
<?php
echo '<script>
some javascript
</script';
?>
However if you are just trying to load the php on page load without any php args then just write it out of the tags.
If you have the JavaScript is another file then using
PHP
<?php
include 'path/to/javascript/file.php';
?>
Should work.
You cannot hide javascript as it is interpreted browser side and not server side so the users browser has to have accesses to the code.
Here is the code I think you are asking for:
PHP HTML
<?php
$html = file_get_contents("path/to/data.html");
?>
<div>
<?php echo $html; ?>
</div>
doesn't use an iframe but should still work. However any relative links will not work unless both files are in the same dir and there will be no iframe functionality without additional css
You can just output it as text in your PHP file.
<?php
// Some PHP stuff here
?>
<script type="text/javascript">
function loaded(){
....
}
onload=loaded;
</script>
<?php
// Some more PHP stuff here
?>
Alternatively, you can just link to it in the usual way from within the same file:
<script src="path/to/your/file.js"></script>