dynamic php header file with dynamic .js and .css include - javascript

I would like to do a dynamic include of my .css and .js file within the head tags in my header.php that depend on the current .php file that is open. So it will save some loading time if the .js or .css file is not needed for the current .php page.
So I thought I check the current url if it includes (strpos) a spicific string (e.g. "mycollection.php") and if yes echo the <link> and <script> tags to include the .css or .js files for the current, opened php file. But it seems to be hard to do a clean script this way. Does anyone has a clean, short and better solution for this problem?
Thank you in advance!

Try code below :
<!DOCTYPE html>
<html>
<head>
<?php
$get_url=$_SERVER['REQUEST_URI'];
$get_extension=explode('/',$get_url);
$get_extension_php=end($get_extension);
if($get_extension_php=='mycollection.php')
{
echo '<link href="your.css"/>';
echo '<script src="your.js"></script>';
}
?>
</head>
<body>
</body>
</html>
Hope it helps you

I used it before but not in the link but before the include of the header and it was like that
if (!isset($css){ echo "<link></link>;}
and at the header of the page if I didn't need to include the css file I just declare the variable $css = ''; and here we go hope this what you were asking for

Related

Include JavaScript files to the document head with php

I'm creating a blog where I included header.php(Header, included in every page). Header contains a section where all the scripts (Scripts that will need in every pages) will be included.
header.php
<!DOCTYPE html>
<html>
<head>
<!-- Scripts Section -->
<script src="example01.js"></script>
<script src="example02.js"></script>
<title>Demo</title>
</head>
<body>
<p>Blog</p>
But in a specific page, I've to include a specific script (file.js named).
index.php
<?php
include('header.php');
importjs('file.js'); /* Any Function To Import JavaScript*/
?>
If I include the JavaScript file here then it'll be included in body not the scripts section in the header. I don't know how to create that function. So please tell me how to create that function. create function in header.php and please first include the header before using function.
Thank You
I think, you should to write your line code like this example:
<?php int a; ?>
<script type="text/javascript" src="file.js" />
<?php int b; ?>

Create an external file for side menu

How do I create an external file for my side menu?
<a class="active" href="C.html" style="font-size: 1.1em;font-weight: 900;">C Programming</a>
C++
C#
Go
Java
JavaScript
PHP
Python
Ruby
Swift
Currently I have these kind of links in every file and the code is just getting too much. Is there a way to create an external file for the menu items?
I tried this but the problem is class="active". I want to highlight a menu item when my menu item is in index.php
I Know how to achieve this when my menu is in each individual HTML file but how do I do that when it's in external PHP file ?
Index.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="CSS/Tags.css">
<link rel="stylesheet" type="text/css" href="CSS/Nav.css">
<link rel="stylesheet" type="text/css" href="CSS/Card.css">
<link rel="stylesheet" type="text/css" href="CSS/Buttons.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="sidenav">
<h1>&nbspOthers</h1>
MS Excel
MS PowerPoint
MS Word
WordPress
</div>
</body>
</html>
Main file
<?php include('index.html'); ?>
You should NOT use javascript to include your menu .
you can use PHP to simply include your menu ( but i sugget header ) file to your page :
<?php include('/template/header.html'); ?>
or you can use pre processors like Gulp :
##include('./template/header.html')
var fileinclude = require('gulp-file-include'),
gulp = require('gulp');
gulp.task('fileinclude', function() {
gulp.src(['index.html'])
.pipe(fileinclude({
prefix: '##',
basepath: '#file'
}))
.pipe(gulp.dest('./'));
});
or Pug or Grunt and ......
About your problem in comments . lets say you are in your main folder and you have this 3 files :
index.php
header.html
homepage.html
footer.html
index.php should be like this :
<?php
include('header.html');
include('hompage.html');
include('footer.html');
?>
and then in your browsers you should open index.php .
** don't use index.html and index.php in the same time .
if it didn't work . you should get error in your index.php file .
when your code works . i suggest you to use readfile() instead of include . if your another files are just html and not have any php codes in it .
As pointed out by Abolfazl Ghaemi :
You should NOT use javascript to include your menu . you can use PHP to simply include your menu ( but i sugget header ) file to your page :
<?php include('/template/header.html'); ?>
The Gulp/Grunt solution is fine but I'll stick with the <?php include(); ?> solution for your problem.
Does the hosting machine speak PHP ?
If browsers can read HTML just fine to give you a webpage, they can't read PHP. Instead, PHP need to be executed by something on the hosting machine and you will need a PHP interpreter for that like Apache.
If you didn't already, I strongly recommend you to install it or even a full AMP stack.
PHP in HTML files won't work
You need to make sure your file using PHP instructions are .php files. Otherwise, the code in your <?php ?> tag (the PHP code) won't be interpreted at all.
Make sure your main file is a .php file.
Are you calling the correct file ?
Also, you need to be sure the file name you are including is correct. The content as shown in your question is
Main file
<?php include('index.html'); ?>
but your file is actually index.php not index.html.
HTML is prettier with CSS
If you want to "highlight" the item with active class, you will need CSS. You can quickly add CSS in your file with a <style> tag. See the example below.
<ul>
<li>Not highlighted</li>
<li>Still not highlighted</li>
<li class="active">Highlighted ! Spotlight's on me baby !</li>
<li>Not highlighted</li>
</ul>
<style>
.active {
color: red;
font-weight: bold;
}
</style>

how do i import a html form into a separate file

I've been trying to import a html form and embedded it into a separate html file, my javascript to try and upload the file containing the form looks like this
<link rel="import" href="formhtml5.html" onload="handleLoad(event)"
onerror="handleError(event)">
var link = document.querySelector('link[rel="import"]');
var el = link.import.querySelector('#flexyForm');
document.body.appendChild(link);
however when I try and see if the form is uploaded my page is just blank? been struggling with this for days now and its driving me nuts, anyone got any guesses to see how it works?
If this is what you're looking for, it's possible with jQuery.
firstFile.html
<html>
<head>
<script src="jquery.js"></script>
<script>
$(function(){
$("#includeForm").load("formFile.html");
});
</script>
</head>
<body>
<div id="includeForm"></div>
</body>
</html>
formFile.html
<p>This is my Form</p>
Note: Your question is not clear, you're not uploading anything. Instead you're just importing some lines of code from another html file to yours. If yes, above code should work fine!
Also, If the included HTML file has CSS attached to it, it might mess up your page style. So better to do internal CSS or change the CSS external link in formFile as per link rules of firstFile.html
You can also use php in additional to JS to get the content of the second file.
<?php file_get_contents="path to your file here" ?>

Include all javascript files belonging to a folder in laravel 5.2

Is there a way I could include all the javascript files belonging to a folder, from a .php file in laravel 5.2?
Something along the lines of:
<script type="text/javascript" src="{!! asset('js/*.js') !!}"></script>
You can use the following code to achieve your goal:
#foreach(Storage::files('/js') as $file)
<script type="text/javascript" src="{!! asset($file) !!}"></script>
#endforeach;
This is the first idea that comes to my mind.
$list = File::allFiles('/path/to/dir');
foreach ($list as $file)
{
echo '<script type="text/javascript" src="' . (string)$file . '"></script>
}
That'd include all files in a folder. That said, it'd probably be a better idea to create a file such as includes or bootstrap, where you list any and all files that you should include - and include THAT file instead. More maintainable that way, and if you wish for a file to just be temporarily "disabled", you wouldn't have to delete or move it.

What's the correct way to write this jquery attr code? (wordpress)

I have this in my theme's function file:
$(document).ready(function() {
$(".ajax-loader").attr("src","<?php bloginfo('template_url'); ?>/images/ajax-loader.gif");
});
However, when it prints, the src prints as <?php bloginfo('template_url'); ?>/images/ajax-loader.gif, that is showing the php code instead of showing my template url. What would be the correct way to write this code?
Is this in a .js file? If so, you can't place WordPress template tags inside because .js files are not processed by PHP.
You can either include your code inline within header.php using <script> tags:
<script type="text/javascript">
$(document).ready(function() {
$(".ajax-loader").attr("src","<?php bloginfo('template_url'); ?>/images/ajax-loader.gif");
});
</script>
Or change the extension of your JavaScript file from .js to .php, and add this line at the very top:
<?php header('Content-Type: text/javascript'); ?>
That tells the server to treat this as a JavaScript file, although it'll be processed by PHP.
In any case, you're using .attr() correctly.

Categories