Created
February 27, 2015 10:12
-
-
Save dpnishant/41a2f85aa6b491d0bfab to your computer and use it in GitHub Desktop.
Conditional Framebusting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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