0

I'm creating a tool that display a banner on a website when a simple javascript line is placed. Something like this is enough to show the banner on any page.

<script type="text/javascript" src="http://mysite.com/mytool/"></script>

Basically its code is something like this

var div = document.createElement('div')
document.write('<div>all content goes here</div>');

Since its a 300 line app there is a lot of CSS involved.

Is there any way to embed an external CSS file when doing this inline HTML rendering via javascript?

I'm not using jQuery or any other library since I'm trying to reduce the output size as possible as I can.

1
  • 1
    Just document.write() the <link> tag. Commented Jul 21, 2012 at 21:04

2 Answers 2

4

You can print the link tag, as @Pointy pointed out:

document.write('<link href=stylesheet href=foo/style.css>');

But better is to place it in the head:

document.head.innerHTML += '<link href=stylesheet href=foo/style.css>';
Sign up to request clarification or add additional context in comments.

2 Comments

scr does not work , it has to be href and docment.write is incorrect in the context
@RabNawaz thank you. I have edited it, can you just remove the -1? Thank you!
3
head = document.getElementsByTagName('head')[0],
style = document.createElement('link');
style.type = 'text/css';
style.href = "pathtocss.css"
style.rel = "stylesheet"
head.appendChild(style);

2 Comments

the style element doesn't have a src attribute: w3.org/TR/html5/the-style-element.html#the-style-element
a link element doesn't have a src attribute too... @RabNawaz w3.org/TR/html5/the-link-element.html#the-link-element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.