Pre-loader with javascript loaded only once

0

Sorry friends what happens is that I have a question with javascript I have a pre-loader and I would like to know how I could make that pre-loader appear only once, what I mean is that the user at the moment of being navigating through the tabs the pre-loader is not appearing every time you change the tab, it loads only once when it is the first time the page is opened I am new in javascript and I do not know how to solve it here I leave the code

<div id="loader"></div>
<div id="content">
    <h1>GIF Pre-Loader</h1>
</div>
<script type="text/javascript">

    var loader;
    function loadNow(opacity) {
        if(opacity <= 0) {
            displayContent();
        }
        else {
            loader.style.opacity = opacity;
            window.setTimeout(function() {
                loadNow(opacity - 0.05)
            }, 100);
        }
    }

    function displayContent() {
        loader.style.display = 'none';
        document.getElementById('content').style.display = 'block';
    }

    document.addEventListener("DOMContentLoaded", function() {
        loader = document.getElementById('loader');
        loadNow(1);
    });

</script>

Help please

    
asked by Juan Angel Hdz 05.12.2018 в 02:33
source

1 answer

0

Let's say you have a html file structure

carpeta
|-index.html
|-pagina1.html
|-pagina2.html
|-pagina3.html

And you want to show only in pagina1.html a way to do it would be doing an if when you are on the page pagina1.html is executed. To achieve this we will use the window.location.pathname that returns the current directory in which you are, then execute a substring to delete the directory and return only the name of the file. In this way we will get the name of the file:

var directorio=window.location.pathname;
var fichero=dir.substring(dir.lastIndexOf('/')+1);
if(fichero=="pagina1.html"){
        document.addEventListener("DOMContentLoaded", function() {
            loader = document.getElementById('loader');
            loadNow(1);
        });
}
    
answered by 05.12.2018 в 04:28