How to link a JS file to a PHP Page Template? - javascript

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/

Related

Refresh PHP code inside DIV with javascript won't work

This question is unique because the error
ReferenceError: $ is not defined comes from the fact that javascript is called in the header but the jQuery is looking inside the parent iframe and don't find it.
I got this page:
review.php
<?php include 'review_core.php'; ?>
<div id="primary" class="content-area col-md-9">
<main id="main" class="post-wrap" role="main">
<?php include 'review_index.php'; ?>
</main><!-- #main -->
</div>
<?php get_sidebar();
get_footer(); ?>
in review_core.php I got some php that fetch folders, images and other things from a S3 bucket, and in review_index.php I got some html and php to show up the data runned by review_core.php inside (a lot) some divs.
The get_sidebar(); just get the wordpress sidebar where I have an Iframe and here is where the problem is coming. In this iFrame is runned a php script (some_php_in_frame.php) that when ends should reload the #primary div, so basically it have to reload review_index.php into it. I'm tring to do it by adding this code at the end of some_php_in_frame.php:
some_php_in_frame.php
<?php [...] ?>
<script>
$(document).ready(function(){
parent.location.href=parent.location.href;
$('#primary').load('review_index.php');
})
</script>
but the refresh don't work and as results in debug I get:
ReferenceError: $ is not defined
Make sure that in the HTML of the page that contains the iFrames, as well as any page that needs to run jQuery in an iFrame imports the jquery library in the <head> section of your HTML document.
The error message on your JavaScript console indicates that this is missing.
For Example:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
$() functions are part of the jQuery Library, and not javascript natively.
The solution was easy:
<script>
$(document).ready(function(){
parent.$(' #primary').load('review_index.php');
})
</script>

How to add lines before each and every </head> in multiple HTML Page site?

I hope all are doing well.
How to add respo_style.css before closing </head> in multiple(in my case there is 65 pages) html + PHP page.
is there any best way to add dynamically
<link rel="stylesheet" type="text/css" href="respo_style.css" />
using code?
In my case i have one config.php which is called in every page.
NOTE: Without add or change any other files. Is this possible to add the lines dynamically using jQuery or JavaScript.
Simple things is just add extra additional file in existing without opening each and every files.
I'm sure there are a lot of ways but here's an explanation for the fastest/easiest implementation IMO.
For code that is repetitive, you should store that code in one file and call it using PHP's include_once function. However in order to use PHP in your files, you would need to use use .php extension instead of .HTML (assuming you files are .HTML) for your webpages.
Here's an example:
<head>
<?php include_once("repetitive_code_here.php"); ?>
</head>
If you run ob_start(); at the beginning of your script, then you can call ob_get_contents() to get the page and replace </head> with your stylesheet.
$string = ob_get_contents();
$link = '<link rel="stylesheet" type="text/css" href="respo_style.css" />';
echo str_replace('</head>', $link . '</head>', $string);
ob_end_clean();
or instead of ob_get_contents pass the funtion to ob_start();
function callback($buffer) {
$link = '<link rel="stylesheet" type="text/css" href="respo_style.css" />';
return str_replace('</head>', $link . '</head>', $buffer);
}
ob_start('callback');
but you will need to call ob_end_flush(); at the end.
This will work. I have style.css in all file. so i can include the respo_style.css in style.css using this code #import url("base.css");
You should do something like this:
page.php (write this in all 65 pages)
<?php
require_once('includes/head.php'); //Assuming you put head.php in a folder called "includes" inside the root
?>
//Then the content of the page
And head.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="respo_style.css" />
</head>

Include script files on codeigniter view file

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

Link Javascript in codeigniter

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

How to put freichat script in as smarty template file

I have How freichat chat script in php and want to integrate with custom php based site with smarty as template engine (View ). How to put that script in smarty template so it works?
Here is code for it...
<script type="text/javascript" language="javascipt"
src="http://server/freichat/client/main.php?id=<?php echo $ses;?>&xhash=<?php echo freichatx_get_hash($ses); ?>">
</script>
<link rel="stylesheet"
href="http://server/freichat/client/jquery/freichat_themes/freichatcss.php"
type="text/css">

Categories

Resources