Hey guys, i want to my javascript function will apply when my windows tab gets small but witout refreshing the page. So, i tried to use AJAX, the function works but i’m stil refreshing my web page.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!--STYLES-->
<link rel="stylesheet" href="styles.css">
<!--JAVASCRIPT-->
<script defer src="script.js"></script>
<script defer src="script2.js"></script>
</head>
<body>
<header>
<img src="images/header.jpg" width="100%" height="150px">
</header>
<nav class="menu">
<button id="button" hidden onclick="toggle()"><img src="images/list.png"></img></button>
<ul id="menu_list">
<li><a href="#">Home</a></li>
<li><a href="#">About me</a></li>
<li><a href="#">Projects</a></li>
<li><a href="#">Blog</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</body>
</html>
header
{
margin-bottom: -3px;
}
.menu
{
width: 10%;
min-width: 200px;
float: left;
}
/*Notes
In the content is rarely set height
*/
/*Links buttons*/
a:link, a:visited {
background-color: #353839;
color: white;
padding: 14px 25px;
text-align: center;
text-decoration: none;
display: block;
border: 1px solid #555d50;
font-size: 1rem;
}
a:hover, a:active {
background-color: #006400;
}
/*Lists*/
ul
{
list-style-type: none;
margin: 0;
padding: 0;
}
/*Best practice font adding with inherit to others elements*/
body
{
font-family: Verdana;
}
input, select, textarea, button
{
font-family: inherit;
}
.hidden
{
display: none;
}
function toggle()
{
let x = document.getElementById("menu_list");
if(x.style.display === "none")
{
x.style.display = "block";
}
else
{
x.style.display = "none";
}
}
//AJAX
function loadFunc() {
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET", "script2.js", true);
xhttp.send();
}
function hide_show()
{
let wh = window.innerHeight;
let ww = window.innerWidth;
re = wh + ww;
//document.getElementById("messenger").innerHTML = re;
if(re <= 1200)
{
document.getElementById("menu_list").classList.add("hidden");
}
if(re <= 1200)
{
document.getElementById("button").style.display = "initial";
}
}
hide_show();