PDA

View Full Version : Help with an easy Javascript...



richmurphy
03-21-2010, 05:14 AM
//EDIT HERE ONLY

var ExitPopURL = 'url'; //This is the URL where your 'exit page' is located.
var AlertBox = "*****************************************************\n\nWait! Stop! Don't Go!\n\nBefore leaving, please leave a comment on the \n\nscript post and share your suggestions.\n\nThanks for visiting AndySowards.com.\n\nAndy \n\n*****************************************************"; // This is what the alert() pop up verbage says.

//DO NOT EDIT BELOW This LINE (Unless of course your Savvy!) ------------------------------



window.onload = function(){
// this is where we start our journey...
createExitPop();
}// end function onunload

function ajaxGET(divId, page, effect)
{
var xmlHttp;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch(e)
{
// Internet Explorer
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
alert("Your browser does not support AJAX!");
return false;
}
}
}

xmlHttp.onreadystatechange=function()
{
if(xmlHttp.readyState==4)
{
if(effect == 'collapse') { document.getElementById(divId).style.display='none'; }
else { document.getElementById(divId).innerHTML=xmlHttp.responseText; }
}
}
xmlHttp.open("GET",page,true);
xmlHttp.send(null);
}

function createExitPop()
{
var theBody = document.getElementsByTagName('body')[0];
var newdiv = document.createElement('div');
newdiv.setAttribute('id','ExitDiv');
theBody.setAttribute('id','body');
newdiv.setAttribute('style', 'width: 100%; height: 100%;');

// put div on page
theBody.appendChild(newdiv);

//add exit pop to page (contents are from your exit.php(or whatever you named it) page)
document.getElementById('ExitDiv').value = ajaxGET('ExitDiv', ExitPopURL);

// style exit pop to resemble its own page
document.getElementById('ExitDiv').style.display = "none";
document.getElementById('ExitDiv').style.top = '0px';
document.getElementById('ExitDiv').style.left = '0px';
document.getElementById('ExitDiv').style.position = 'relative';
document.getElementById('ExitDiv').style.backgroundColor = '#FFFFFF';

}// end createExitPop


isExit = true;


function ExitPop(isExit) {
if(isExit != false) {
isExit=false;
isPop = true;

var bodyTag = document.getElementById? document.getElementsByTagName("BODY")[0] : document.body;

// add id="body" so that it can be referenced.
bodyTag.setAttribute("id", "body");

//replace body text with exit pop
bodyTag.innerHTML = document.getElementById('ExitDiv').innerHTML;
return AlertBox;
} // end if
}// end function

window.onbeforeunload = function(){

// Lay down an exit pop!!
return ExitPop(isExit);

}// end function onunload

I am just trying to get this javascript to decipher between internal and external links, and in the event it is an internal link I would like to override the popup script so the visitors aren't bugged with a popup on every clicked internal link.

I found some code on here that is...



function checkUrl(href) {
leaving = ((href.indexOf("://") > -1) || (href.indexOf("www.") > -1));
}


...but I couldn't get it to work with my script. Please help! Thanks guys, and girls!

jscheuer1
03-21-2010, 05:26 PM
There are at least two problems here. The code at the bottom of your post won't work. All links will pass that test, even those to your own site. That's one, the other is when/how to use it if it did work.

Forget about whether the link is local or not for a moment. Let's say we prevent the user from leaving and present this new content and they do everything we ask of them. Then they go to leave for real, can they?

In any case, this code can tell which type of link was clicked:


(function(){
var links = {
addEvent: (function(){return window.addEventListener? function(el, ev, f){
el.addEventListener(ev, f, false);
}:window.attachEvent? function(el, ev, f){
el.attachEvent('on' + ev, f);
}:function(){return;};
})(),
check: function(e){
e = e || event;
var t = e.target || e.srcElement;
if(t == t.href && t.href.indexOf(loc) > -1){ //link is local
window.leaving = false;
} else { //link is off site
window.leaving = true;
}
}
}, loc = location.host.replace(/www./, '');
links.addEvent(window, 'load', function(){
var a = document.getElementsByTagName('a');
for (var i = a.length - 1; i > -1; --i){
links.addEvent(a[i], 'click', links.check);
}
});
})();

It will react before unload, even before beforeunload, so the leaving variable will be set for use in your other functions.