0

I have page that automatically once the window height falls beneath a certain size (450px).

Question is - how do I retain the content once the window height goes above a certain size (450px).

I've tried .remove but it doesn't work.

Code:

$(window).resize(function() {
    if ($(window).height() < 450) {
        $('body').html("Please resize your screen.");
    }
    else {
        $('body').remove(this);
    }
});

Jsfiddle: http://jsfiddle.net/zyL6fyvn/1/

methuselah
  • 12,766
  • 47
  • 165
  • 315

1 Answers1

1

Easiest (and I think only) way, is to store the content into a variable before you remove it:

$(window).on('load',function() {
    var body;
    var bodyVisible = true;

    function resize() {
        if (bodyVisible==true && $(window).innerHeight()<450) {
            bodyVisible = false;
            body = $('body').html(); //store current body content before removing it
            $('body').html("Please resize your screen.");
        } else if (bodyVisible==false && $(window).innerHeight()>=450) {
            bodyVisible = true;
            $('body').html(body); //restore body content
        }
    }
    resize(); //invoke resize() on page-load
    $(window).resize(resize); //invoke resize() on page-resize
});

fiddle: http://jsfiddle.net/zyL6fyvn/3/

  • See comments in the code above for explanation.
  • I put the resize code inside a standard function that is invoked once on page-load and after that on every page-resize. (Otherwise the content will still show if the window's height is below 450, until you have resized once.)
  • I added the bodyVisible boolean, otherwise the body content is rewritten again on every resize. Now it's only rewritten once when the height passes the threshold.
  • I prefer to use .innerHeight() instead of just .height(), I get more consistent and reliable results with that. But that's just my opinion.
  • In your HTML, try to close your <input /> and your <br /> elements (the space before the slash is no typo).

(I have never seen a <br /> with a class btw, or CSS being applied to it for that matter.. but apparently it works, and it's even officially valid:)

Community
  • 1
  • 1
myfunkyside
  • 3,890
  • 1
  • 17
  • 32