0

I have a webpage that I am creating, and I am trying to add a link to the top that will add a css file. I think that it is not working because I load the page, then add the link element and it never actually puts the styles found in the css file to the elements. I tried having it reload the page using location.reload(), but that took the link element away from the page. Any suggestions on how to do this would be great. I would prefer not to use jquery, but I can if needed.

Current Code:

function findCss() {
    var fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", "styles/index.css")
}

window.onload = findCss;

1 Answer 1

2

Try adding

document.body.appendChild(fileRef);

or

document.head.appendChild(fileRef);

at the end of that function, depending on where you want the link to be put (header or body).

Sign up to request clarification or add additional context in comments.

Comments

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.