jquery page load event

Hi experts,

I'm using jQuery on a html page.
I'm using this version of jQuery  http://code.jquery.com/jquery-1.10.1.js

In my page jquery code I have this code.
This code shows a div called #div1 whenever the page load.

    // after page refresh this fires
    $(window).load(function() {
     //insert all your ajax callback code here.
     //Which will run only after page is fully loaded in background.
                        
                // show div called div 1 on page load
            $("#div1").show();      
    });



On my page i have two divs. One has id="div1" and the other has id="div2".

Is there a way to do this?:

On the very first page load show #div1.

On the 2nd page load or any other page load after the 2nd page load show #div2 and hide #div1
LVL 1
maqskywalkerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
On the 2nd page load
What do you mean by page load? Does this mean a page refresh (F5?)

If so you are resetting state so if you want the behaviour you are describing you need to maintain some sort of state to say what has happened in the past.

My question is if you are going to do this (maintain state) then why not simply have the the server process either render out div1 or div2 depending on the state?

I suspect I am not understanding your requirement entirely - please can you elaborate a bit more.
0
Marco GasiFreelancerCommented:
I assume you want this: the first time a page is loaded you want to show div1, all next times you want to hide div1 and show div2. If this is your question you can accomplish this just setting a cookie. First, download jquery.cookie plugin from here: https://github.com/carhartl/jquery-cookie

Then use this code:
    // after page refresh this fires
    $(window).load(function() {
     //insert all your ajax callback code here.
     //Which will run only after page is fully loaded in background.
            var yet_loaded = $.cookie("maqskywalker");
            if (yet_loaded === null)
            {
                // show div called div 1 on page load
                $("#div1").show();      
                $.cookie("maqskywalker", "loaded yet");      
            }
            else
            {
                $("#div1").hide();      
                $("#div2").show();      
            }
    });

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maqskywalkerAuthor Commented:
Thanks.
0
Marco GasiFreelancerCommented:
Thanks for points
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.