Is there a way I can use Javascript to include a CSS file only if the top frame URL contains the string "facebook.com"?
Short pseudo code:
if top.frame.url.contains("facebook.com"):
include("style-facebook.css");
A quick document.write-based solution would be:
<script type="text/javascript">
if (/facebook\.com/.test(window.top.location.host)) {
document.write('<link rel="stylesheet" type="text/css" href="stylefacebook.css" />');
}
</script>
Or using the dom:
<script type="text/javascript">
if (/facebook\.com/.test(window.top.location.host)) {
var lnk = document.createElement('link');
lnk.type='text/css';
lnk.href='stylefacebook.css';
lnk.rel='stylesheet';
document.getElementsByTagName('head')[0].appendChild(lnk);
}
</script>
Or with jquery:
<script type="text/javascript">
if (/facebook\.com/.test(window.top.location.host)) {
$('head:first').append('<link rel="stylesheet" type="text/css" href="stylefacebook.css" />');
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With