0
Good afternoon, I am making a website apênas with pure Html5 and Css3 and in it has a menu, however I want that menu stay fixed in all other pages and then not to need to copy and paste the code in all pages I decided to use a little Java Script, however when opening other pages (other than the Index that was the page that the menu was made) the browser does not load and displays an error message.
Below is the picture of the error that is appearing in the browser. When opening the site on a local server it "Download" the entire menu and does not apply the formatting of the Css that was supposed to occur, not to mention that on other pages the menu does not even appear. Photo from the menu as it was to actually appear
The code below refers to the menu made in Html
<body>
<div class="container">
<div class="item1 bg-color-item1">
<ul class="texto">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="github.html">Github</a>
</li>
<li>
<a href="#linkedin">Linkedin</a>
</li>
<li>
<a href="contact.html">Contact</a>
</li>
</ul>
</div>
The code below refers to the stylized menu with Css
ul {
display: flex;
box-sizing: border-box;
text-decoration: none;
justify-content: space-around;
padding: 18px 400px;
}
li {
display: inline-block;
font-family: 'Oswald';
padding: 5px 0px;
}
.bg-color-item1{
background-color: rgb(65, 63, 63);
}
.item1 {
position: fixed;
color: aliceblue;
border-bottom: solid 5px rgb(67, 139, 233);
width: 100%;
}
a {
text-decoration: none;
color: #eee;
font-size: 20px;
}
.item1 a {
padding: 10px;
}
.item1 a:hover {
box-sizing: border-box;
background-color: rgb(67, 139, 233);
margin: 15px;
transition: 0.5s;
}
Below is the code in Java Script
window.onload = function () {
var template = `
<body>
<div class="container">
<div class="item1 bg-color-item1">
<ul class="texto">
<li>
<a href="home.html">Home</a>
</li>
<li>
<a href="about.html">About</a>
</li>
<li>
<a href="github.html">Github</a>
</li>
<li>
<a href="#linkedin">Linkedin</a>
</li>
<li>
<a href="">Contact</a>
</li>
</ul>
</div>
<div class="item2"></div>
<div class="item3"></div>
<div class="item4"></div>
</div>
</body>
`;
document.getElementById("header").innerHTML = template;
}
Thanks in advance !
Part of html where I call js
<body>
<div id="header"></div>
<div class="container">
<div class="main">
<div class="flip-card">
<div class="flip-card-inner">
<div class="flip-card-front giovanni">
<img src="img/giovanni.jpg" alt="Giovanni">
</div>
<div class="flip-card-back">
<h1 class="text-flip-card">Nome: </h1>
<h4 class="text-flip-card">Idade: </h4>
<h4 class="text-flip-card">Sexo: </h4>
<h4 class="text-flip-card">Estado civil: </h4>
</div>
</div>
</div>
</div>
<div class="texto sombra-texto">
<p>
</p>
</div>
</div>
<script type="module" src="./header.js"></script>
On the screen you are calling the menu you have an element with the ID
header
? You are calling the header.js inside the <head> or at the end of the document (I mean at the end of the body)?– hugocsl
Place the HTML of the page.
– Sam
hi, sorry I forgot to put the html part where I call the js, I just updated the code, Thank you !
– Pedro
There are two points you have to take into consideration by applying this approach. First: you will always have to have in all your pages a div com with id header According to the header.js file you have to be in the same folder as the html file you are opening.
– Ricardo Gomes
@Ricardogomes I did exactly so I put in all the pages and this in the same file I’m opening but it still doesn’t work, you know what can be ?
– Pedro
the problem is that you are putting the <body> tab in the template, if it will load on other pages as menu put only reusable code
– Wilson Rosa Gomes