Como evitar que el usuario salga de la página

Lo que estoy buscando es como hacer que en el momento en el que el usuario clickee el boton "atrás" o la cruz para cerrar la página, salga una alerta con un texto de mi elección y con la opción de quedarse en la página, y ser redireccionado a otra página, o salirse de la definitivamente de la página. A ver si alguien me ayuda con esto :D.
Supongo que con javascript no es complicado hacerlo, aunque la verdad no sé lo suficiente como para crearlo yo mismo.

Lo de la ventanita, si mal no recuerdo, se hacía con algo de alert (lo tengo en un librillo :) ) y antes habría que hacer que se habriese al clickar atrás, humm, voy ver si encuentro algo.

EDITO: Pues no lo encuentro, lo más parecido fue un código para que al pulsar atrás redireccione a otra página. No sería mejor que fueses a forojavascript.com? Aquí no creo que haya mucha gente que sepa :P
La verdad es que no tenía ni idea de en que lenguaje estaba hecho, simplemente lo vi en un página, me gusto la idea y quería copiarlo para algunos proyectos míos. Gracias por el dato, buscaré algún foro de javascript a ver si alguien me puede ayudar por ahí.
Vaith, puedes decir la página donde lo viste? Es que seguramente se puede copiar el código directamente de ella, me imagino que tendrán un archivo .js como si fuera una hoja de estilos css.
jeje, lo tengo:

<script language="javascript">
var exitsplashalertmessage = '***************************************\n\n         > > > W A I T < < <\n\n     CLICK THE ***CANCEL*** BUTTON\n    on the NEXT Window for Something\n             VERY Special!\n\n***************************************';
var exitsplashmessage = '***************************************\n\n W A I T   B E F O R E   Y O U   G O !\n\n  CLICK THE *CANCEL* BUTTON RIGHT NOW\n     TO STAY ON THE CURRENT PAGE.\n\n I HAVE SOMETHING VERY SPECIAL FOR YOU!\n\n***************************************';
var exitsplashpage = 'http://www.exitsplash.com/exit/exit.php?hop=ehotitems';
</script>
  [b]<script language="javascript" src="http://www.exitsplash.com/exit/exitsplash.php?tc=3399cc&ic=ffffff&ua=1&uh=3&ad=6&sh=no&bh=22&fs=12&ia=center&lf=Arial&at=Powered%20by%20ExitSplash"></script>[/b]

Lo que te puse en negrita incluye otro código, tendrías que cambiar el src="....." por src="archivo.js". En ese archivo .js incluye esto:

function addLoadEvent(func) { var oldonload = window.onload; if (typeof window.onload != 'function') { window.onload = func; } else { window.onload = function() { if (oldonload) { oldonload(); }  func(); }}}
function addClickEvent(a,i,func) { if (typeof a[i].onclick != 'function') { a[i].onclick = func; } }
var theDiv = '<div id="ExitSplashDiv" onmouseover="javascript:hideexitcancelbuttonimage();StopExitImpactAudio();" style="display:block; width:100%; height:100%; position:absolute; background:#FFFFFF; margin-top:0px; margin-left:0px;" align="center">';
theDiv = theDiv + '<div id="ExitCancelButtonImageDiv" style="background: #ffffff; width: 100%;" align="center">[img]http://exitsplash.s3.amazonaws.com/files/exitimpactimage3.gif?exit4bc8fb5e04567[/img]</div>';
theDiv = theDiv + '<div id="exitsplashimpactaudio"><embed wmode="transparent" src="http://exitsplash.s3.amazonaws.com/player.swf?url=http://exitsplash.s3.amazonaws.com/files/exitsplash6.mp3&mode=play&autostart=true&exit4bc8fb5e04567" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="1" height="1"></embed></div>';
theDiv = theDiv + '<iframe src="'+exitsplashpage+'" width="100%" height="100%" align="middle" frameborder="0"></iframe>';
theDiv = theDiv + '</div>';
theBody = document.body; if (!theBody) {theBody = document.getElementById("body"); if (!theBody) {theBody = document.getElementsByTagName("body")[0];}}
var PreventExitSplash = false;
function DisplayExitSplash(){ if(PreventExitSplash == false){ window.scrollTo(0,0); window.alert(exitsplashalertmessage); PreventExitSplash=true; divtag = document.createElement("div"); divtag.setAttribute("id","ExitSplashMainOuterLayer"); divtag.style.position="absolute"; divtag.style.width="100%"; divtag.style.height="100%"; divtag.style.zIndex="99"; divtag.style.left="0px"; divtag.style.top="0px"; divtag.innerHTML=theDiv; theBody.innerHTML=""; theBody.topMargin="0px"; theBody.rightMargin="0px"; theBody.bottomMargin="0px"; theBody.leftMargin="0px"; theBody.style.overflow="hidden"; theBody.appendChild(divtag); return exitsplashmessage; } }
var a = document.getElementsByTagName('A'); for (var i = 0; i < a.length; i++) { if(a[i].target !== '_blank') {addClickEvent(a,i, function(){ PreventExitSplash=true; });} else{addClickEvent(a,i, function(){ PreventExitSplash=false;});}}disablelinksfunc = function(){var a = document.getElementsByTagName('A'); for (var i = 0; i < a.length; i++) { if(a[i].target !== '_blank') {addClickEvent(a,i, function(){ PreventExitSplash=true; });} else{addClickEvent(a,i, function(){ PreventExitSplash=false;});}}}
hideexitcancelbuttonimage = function(){ document.getElementById('ExitCancelButtonImageDiv').style.display='none'; }
StopExitImpactAudio = function(){ var esDiv = document.getElementById('ExitSplashDiv'); var esiaDiv = document.getElementById('exitsplashimpactaudio'); esDiv.removeChild(esiaDiv); }
addLoadEvent(disablelinksfunc);
disableformsfunc = function(){ var f = document.getElementsByTagName('FORM'); for (var i=0;i<f.length;i++){ if (!f[i].onclick){ f[i].onclick=function(){ PreventExitSplash=true; } }else if (!f[i].onsubmit){ f[i].onsubmit=function(){ PreventExitSplash=true; }}}}
addLoadEvent(disableformsfunc);
window.onbeforeunload = DisplayExitSplash;

Si no me equivoco, el segundo código (el del archivo.php) es el que controla lo que se ve una vez sale la 1ª ventanita de alerta y se le dá a aceptar. Eso sí, yo ya no te sabría decir cómo adaptar el segundo código a tu gusto, no sé javascript casi.

Saludos ;)
Vaya, muchas gracias por el código realmente me has ayudado bastante. Ya te devolveré el favor cuando necesites algo. Veamos si jugando con el código consigo modificarlo a mi gusto.
Vaiht Lumin escribió:Ya te devolveré el favor cuando necesites algo.

jeje, eso dice mucho de ti :wink:

Vaiht Lumin escribió:Veamos si jugando con el código consigo modificarlo a mi gusto.

Eso es lo chungo, ojalá lo consigas, si no consigues nada, igual puedo intentarlo yo pero ya te digo que no sé casi javascript :P