Download JavaScript Code Examples, Tutorials, Reference

Provide thousands of free JavaScript code examples, DHTML, HTML JavaScript Tutorials, Reference and Help. JavaScriptBank.com is the best JavaScript resources




Over 2000+ free Javascript
at JavaScriptBank.com Website

Sampled by JavaScriptBank.com Do not move your mouse or press a key for 5 seconds to see effect.

The following two scripts create a screensaver type effect.
Page saver1.htm contains the first script to monitor for one of three events, mousemove, mousedown, and keypress, if none of these events are detected after the set time then a fullscreen window is opened.

Note:- Page saver1.htm only serves for this example as the "Screensaver Part 1" script can be placed in any page of your choosing.

Page saver2.htm must be used and is loaded into the fullscreen window, this contains the second script "Screensaver Part 2" which is needed to tell the "Screensaver Part 1" script not to run again until the fullscreen has closed.
The "Screensaver Part 2" script also monitors for the same three events and if detected closes the fullscreen window and allows the "Screensaver Part 1" script to begin monitoring again.

In addition saver2.htm should also contain the script that will run your chosen effect.
Screensaver Part 1 is suspended when its window is not in focus to prevent the script from running if another window is opened.

 

In page saver1.htm

<script language="JavaScript">
<!--
// ********** Screensaver Part 1 **********
// Realised by apacheJeff

Timeout=5000 // delay for effect
Timer=""

function oStatic() {
clearTimeout(Timer)
if(timerRunning == true||blurred==1){ // if win1 opened or opener is blurred, return
return
}
timerRunning = true
win1=window.open("opened.htm",'','fullscreen') // if win1 not opened, open win1
Timer=setTimeout("oStatic()",Timeout) // run function oStatic after delay
}

opened=0 // win1 not opened

function oActive(){
clearTimeout(Timer)
if(opened==1){return} // if win1 opened, return
timerRunning = false
Timer=setTimeout("oStatic()",Timeout) // if win1 not opened run function oStatic after delay
}

document.onmousemove=oActive // detect mouse movement
document.onmousedown=oActive // detect if button pressed
document.onkeypress=oActive // detect if key press

setTimeout("oActive()",1000) // start
//-->
</script>

<BODY onblur="blurred='1'" onfocus="blurred='0';oActive()">

In page saver2.htm.

<script language="JavaScript">
<!--
// ********** Screensaver Part 2 **********
// Realised by apacheJeff
// www.huntingground.freeserve.co.uk

function init(){
opener.opened=1
ignore=0 // ignore busy cursor when page loads
document.onmousemove=gone // detect mouse movement document.onmousedown=gone // detect if button pressed
document.onkeypress=gone // detect if key press
}

function gone(){
if(ignore==0){
ignore=1
return}
opener.opened=0
self.close()
}
document.onmousemove=gone // detect mouse movement
document.onmousedown=gone // detect if button pressed
document.onkeypress=gone // detect if key press

setTimeout("init()",1000)
//-->
</script>