Skip to content

Instantly share code, notes, and snippets.

@struberg
Created December 16, 2010 11:33
Show Gist options
  • Save struberg/743303 to your computer and use it in GitHub Desktop.
Save struberg/743303 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
function getFullUrl(url) {
var urlparts = url.split(/&amp;|\?/g);
var urlpath = urlparts[0];
if (urlpath.indexOf('://') &lt; 0) {
if (urlpath[0]== '/') {
return urlpath;
}
else {
var startpathloc = window.location.pathname.lastIndexOf('/');
return window.location.pathname.substring(0, startpathloc+1) + urlpath;
}
}
return url;
}
function addWindowCookie(url) {
var windowId = window.name;
if (windowId == undefined) {
return;
}
// 2 seconds expiry time
var expdt = new Date();
expdt.setTime(expdt.getTime()+(1*1000));
var expires = "; expires="+expdt.toGMTString();
var fullurl = getFullUrl(url);
var pathName = encodeURIComponent(fullurl.replace(/\//g, ""));
document.cookie = pathName + '-codiWindowId=' + windowId + expires;
}
window.onload = function() {
var anchors = document.getElementsByTagName("a");
var i=0;
for (i=0;i&lt;anchors.length;i++) {
var a = anchors[i];
if (a.getAttribute('onclick') == undefined) {
var url = a.getAttribute('href');
a.setAttribute('onclick', "addWindowCookie('" + url + "'); return true;")
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment