1

Consider the following,

<div id="outerDIV">
     <div id="innerDIV" style="height: 100%;">
         <asp:ContentPlaceHolder... />
     </div>
</div>

These DIVs reside in the master page of an asp.net site. I would like to write a function that copies the innerDIV's height and apply it to the outerDIV's height. I can do this normally, like so:

    $(document).ready(function () {
        $('#outerDIV').height($('#innerDIV').height());
    });

but the problem is I only want it to occur at the first load of the page. When the user navigates away to other pages, I don't want the outerDIV to resize again.

How may I accomplish this?

Thanks

Carlos Mendieta
  • 860
  • 16
  • 41

3 Answers3

1

From the MDN Docs: Use a class selector in conjunction with your ID selector.

 $(document).ready(function(){
         $('#outerDIV.firstpage').height($('#innerDIV.firstpage').height());
     }
 });

By adding the class='firstpage' selector, you can differentiate between your first page and your other pages, without worrying about cookies or tracking another variable.

Tui Popenoe
  • 2,098
  • 2
  • 23
  • 44
0

Maybe you could do something like this

var applied = 0;/*use it to check if height to outter div has been applied */
$(document).ready(function(){

  if(applied == 0){
  $('#outerDIV').height($('#innerDIV').height());
applied = 1;
}//this way the height will be applied only once

});
-1

Why don't you use cookies.

    $(document).ready(function () {
        if(!$.cookie("setHeight"))
        {
           $('#outerDIV').height($('#innerDIV').height());
           $.cookie("setHeight", "1");
        }
    });

And delete the cookie whenever you want using $.removeCookie("setHeight");

Tui Popenoe
  • 2,098
  • 2
  • 23
  • 44
void
  • 36,090
  • 8
  • 62
  • 107
  • 2
    Very important read: [cookies vs localStorage](http://stackoverflow.com/questions/3220660/local-storage-vs-cookies). In short, don't set cookies unless server needs to see them! – Scott Feb 19 '15 at 19:05
  • 1
    Like Jaxo said, you should not be using cookies for something you can control client side with HTML classes and jQuery selectors. – Tui Popenoe Feb 19 '15 at 20:50