0

I tried this function to disable page if it is not on focus.

$(window).focus(function() {
  $("body").show();
}).blur(function() {
  $("body").hide();
});

it works, the problem is depending where user click on the page (it will still focus, since user clicks on the page) the body will hide and will show again when user click back.

Another problem is when user click in the address bar, it will hide the body too. Can I solve this things? what is wrong?

RGS
  • 4,062
  • 4
  • 31
  • 67
  • 2
    I don't understand what you're trying to accomplish with this code, but I'd suggest not using `focus` on `window` - that's just too high level. Also, showing and hiding the `body` tag is not commonly used - I can't think of a case for doing that. I suggest at least creating a `div` as the first child of `body` and put everything you want to hide within that. Perhaps put your focus/blur code on that element too – K Scandrett Apr 28 '17 at 00:15
  • 1
    What is the goal? Focus is usually used for singular things... - and not whole pages. – sheriffderek Apr 28 '17 at 00:17
  • I'm trying to avoid print screen on the page. like here: http://stackoverflow.com/questions/3130983/stop-user-from-using-print-scrn-printscreen-key-of-the-keyboard-for-any-we – RGS Apr 28 '17 at 00:19

2 Answers2

1

I think you should put these in a div tag, maybe your address bar is inside body bar.

<div>
    //something else
</div>
<address>
    //address bar
</address>
Sun Geng
  • 21
  • 2
  • "The HTML `
    ` element supplies contact information for its nearest `
    ` or `` ancestor." https://developer.mozilla.org/en-US/docs/Web/HTML/Element/address
    – Badacadabra Apr 28 '17 at 00:44
1

To prevent hiding body and showing again.

$(window).focus(function(){
 $("body").show();
}).blur(function(){
   // check whether the click is not in body and make it blur.  
})

I don't think there is a way to detect user click in addressbar. But you can always create a separate window using window.open without addressbar. Hope this helps.

NSK
  • 31
  • 5