Skip to content

Instantly share code, notes, and snippets.

@dpnishant
Created February 27, 2015 10:12
Show Gist options
  • Save dpnishant/41a2f85aa6b491d0bfab to your computer and use it in GitHub Desktop.
Save dpnishant/41a2f85aa6b491d0bfab to your computer and use it in GitHub Desktop.
Conditional Framebusting
function matchInArray(arr, subject) {
var parser = document.createElement('a');
parser.href = subject; //using DOM-trickery to parse URLs
if(Object.prototype.toString.call(arr) !== '[object Array]') {
arr = arr.split(); //array typecast
}
if((new RegExp('\\b' + arr.join('\\b|\\b') + '\\b')).test(parser.hostname)) {
return true;
} else {
return false;
}
}
function allowFrameFor(arr) {
try {
if (window.top !== window.self || !matchInArray(arr, window.top.location.href)) {
document.write = "";
window.top.location = window.self.location;
setTimeout(function() {
document.body.innerHTML = '';
}, 0);
window.self.onload = function() {
document.body.innerHTML = '';
};
}
} catch (err) {}
}
var whitelisted = [ //example whitelisted domains; replace with yours
'google.com',
'google.co.in',
'yahoo.com',
'yahoo.in'
];
allowFrameFor(whitelisted);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment