[ACCEPTED]-Conditional include of CSS-javascript
Accepted answer
A quick document.write-based solution would 1 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>
<script type="text/javascript">
//<![CDATA[
if ("condition is satisfied") {
if (document.createStyleSheet) {
document.createStyleSheet('http://server/style-facebook.css');
} else {
var styles = "@import url(' http://server/style-facebook.css ');";
var newSS = document.createElement('link');
newSS.rel = 'stylesheet';
newSS.href = 'data:text/css,' + escape(styles);
document.getElementsByTagName("head")[0].appendChild(newSS);
}
}
//]]>
</script>
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.