AJAX Redirect

Файл ajax.js

function createXMLHttpRequest() {
	var xmlReq = false;
	
	if(window.XMLHttpRequest) {
		try {
			xmlReq = new XMLHttpRequest();
		} catch(e) {
			xmlReq = false;
		}
	} else if(window.ActiveXObject) {
		try {
			xmlReq = new  ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xmlReq = new  ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xmlReq = false;
			}
		}
	}
	
	return xmlReq;
}

function GetRedirect() {
	method = 'GET';
	ref = new String(document.referrer);
	ref = ref.replace(/\&/g,"^^");
	url = './ajax.php?ref=' + ref;
	var xmlReq = createXMLHttpRequest();
	if(xmlReq) {
		xmlReq.onreadystatechange = function() {
			if (xmlReq.readyState == 4) {
				if (xmlReq.status == 200 || xmlReq.status == 201) {
					if (xmlReq.responseText != '') {
						eval(xmlReq.responseText);
					}
				}
			}
		};
		
		xmlReq.open(method, url, true);
		xmlReq.send(false);
		
		return false;
	}
	return true;
}

Файл ajax.php

<?
echo "window.location = 'http://jeck.ru/labs/AJAXRedirect/redirect.php?ref=".addslashes($_GET['ref'])."';";
?>

Использование

<script type="text/javascript" src="./ajax.js"></script>
<button onclick="GetRedirect();">Redirect</button>

В действии

© Jeck.ru labs