Home » Jquery » javascript – Create an external file for side menu-Exceptionshub

javascript – Create an external file for side menu-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

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>
<a href="C++.html" style="font-size: 1.1em;font-weight: 900;">C++</a>
<a href="CSharp.html" style="font-size: 1.1em;font-weight: 900;">C#</a>
<a href="Go.html" style="font-size: 1.1em;font-weight: 900;">Go</a>
<a href="Java.html" style="font-size: 1.1em;font-weight: 900;">Java</a>
<a href="JavaScript.html" style="font-size: 1.1em;font-weight: 900;">JavaScript</a>
<a href="PHP.html" style="font-size: 1.1em;font-weight: 900;">PHP</a>
<a href="Python.html" style="font-size: 1.1em;font-weight: 900;">Python</a>
<a href="Ruby.html" style="font-size: 1.1em;font-weight: 900;">Ruby</a>
<a href="Swift.html" style="font-size: 1.1em;font-weight: 900;">Swift</a>

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>
 <a href="" style="font-size: 1.1em;font-weight: 900;">MS Excel</a>
 <a href="" style="font-size: 1.1em;font-weight: 900;">MS PowerPoint</a>
 <a href="" style="font-size: 1.1em;font-weight: 900;">MS Word</a>
 <a href="" style="font-size: 1.1em;font-weight: 900;">WordPress</a>
</div>

</body>
</html>

Main file

<?php include('index.html'); ?>
How to&Answer:

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 .

Answer:

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>