SiteExperts.com Logo Home | Community | Developer's Paradise | Jobs
User Groups | Site Tools | Site Information | Search

Inside Technique : Leaving the past behind : Code and Limitations

The script for the global solution is provided below. With this script, all links on your web-page will replace the current page in the history.

<SCRIPT>
function getTag(el,str) {
	while ((el!=null) && (str.indexOf(el.tagName + ":")<0))
		el = el.parentElement
	return el
}

function navigateTo(sURL,target) {
        if ((target == '_self') || (target=="")) {
		window.location.replace(sURL);
		return false;
	}
	if (target == '_top') {
		top.window.location.replace(sURL);
		return false
	}
	if (target =='_parent') {
		parent.window.location.replace(sURL);
		return false;
	}
	if (target == '_blank' || parent.frames.length < 1) {
		window.open(sURL, target);
                return false;
	}
	else {
		if (parent.frames[target])
			parent.frames[target].location.replace(sURL);
                else
			window.open(sURL, target);
                return false;
	}
}

function checkIEClick() {
	var el = getTag(event.srcElement,"A:AREA:")
	if ((el!=null) && ((el.tagName=="A") || (el.tagName=="AREA"))) {
		event.returnValue = false
		navigateTo(el.href,String(el.target).toLowerCase())
	}
}

function checkNSClick(ev) {
	if (ev.target.href) {
		navigateTo(ev.target.href,String(ev.target).toLowerCase())
		return false
	}

}

if ((document.all) || (document.layers))
	if (document.layers) {
		document.captureEvents(Event.CLICK)
		document.onclick = checkNSClick
	}
	else
		document.onclick = checkIEClick
</SCRIPT>

This solution has a few limitations. This script supports targeting except when the target frame is more than one layer removed (eg., multiply nested layers or IFrames) - Thanks to Dan Haddix for the targeting code. Also, do not rely on the user not being able to back up (eg., in a game where backing up would enable cheating). The user can always choose open to open the link in a new window using the right-mouse button and then go back to the original window and choose a different option.

Discuss and Rate this Article

Page 1:Leaving the past behind
Page 2:Code and Limitations