Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if page is loaded for the first time using javascript

I want to check if the page is being loaded for the first time, and if it is then display the filter. If I put showFiltermenu() in the pageLoad function then it will show every time the page is loaded but I just want it display the first time. I tried using Page.IsPostBack but that doesn't display the filter.

    <script type="text/javascript">
        function showFiltermenu() {
            $("#filtermenuDrop").toggle('fold', {}, 500);
        }
        function closefiltermenu() {
            $("#filtermenuDrop").toggle('fold', {}, 500);
        }

function pageLoad() {
        $("input[rel^='datepicker']").datepicker({
            dateFormat: 'dd/mm/yy',
            changeMonth: true,
            yearRange: "c-50:c+50",
            changeYear: true,
            showOn: "both",
            firstDay: 1,
            buttonImage: "../images/icons/buttons/basic1-049-small.png"
        });
        <% if (Page.IsPostBack)
           { %>
                showFiltermenu();
        <% } %>

        ShadowboxInit();

    }
like image 371
user123456789 Avatar asked Jul 24 '15 08:07

user123456789


People also ask

How do I know when a JavaScript page is loaded?

After you have a basic understanding of javascript, you can detect when a page has loaded by using the window. onload event. window. onload = function() { addPageContents(); //example function call. }

How do I make sure the page is fully loaded JavaScript?

What is the best way to make sure javascript is running when page is fully loaded? If you mean "fully loaded" literally, i.e., all images and other resources downloaded, then you have to use an onload handler, e.g.: window. onload = function() { // Everything has loaded, so put your code here };

How do you check if an element has been loaded on a page before running a script?

To check if an element has been loaded on a page before running a script with JavaScript, we can use the mutation observer. to create a MutationObserver instance. Then we call observer. observer with the element we want to observe and an object that sets the items we want to observe,.

What loads first JavaScript or HTML?

The browser loads the html (DOM) at first. The browser starts to load the external resources from top to bottom, line by line. If a <script> is met, the loading will be blocked and wait until the JS file is loaded and executed and then continue.


1 Answers

with localStorage you can save values like this:

var firstTime = localStorage.getItem("first_time");
if(!firstTime) {
    // first time loaded!
    localStorage.setItem("first_time","1");
}

no jQuery, no plugins, just pure, beautiful and fast HTML5 API

like image 73
Santiago Hernández Avatar answered Nov 09 '22 07:11

Santiago Hernández