2

i currently have been looking at the following:

if (window.top!=window.self) { 
    <link rel="stylesheet" href="http://www.website.com/content/themes/theme/style.css" type="text/css" />
} else { 
    <link rel="stylesheet" href="http://www.website.com/content/themes/theme/iframe.css" type="text/css" />
}

I want to add linked files, so when it loads on the website styles.css will load, and when in a frame it will load frame.css what am i doing wrong?

3
  • 1
    You're mixing JS with HTML. Commented Mar 31, 2014 at 16:48
  • you are mixing javascript with HTML. Commented Mar 31, 2014 at 16:48
  • @rupps, are you my evil twin? Commented Mar 31, 2014 at 16:49

1 Answer 1

1

You are mixing JavaScript with HTML. That's why you will get syntax errors.

You need to use a function that will load a CSS file:

function loadCSSFile (filename) {
      var fileref = document.createElement("link");
      fileref.setAttribute("rel", "stylesheet");
      fileref.setAttribute("type", "text/css");
      fileref.setAttribute("href", filename);
      if (typeof fileref != "undefined");
         document.getElementsByTagName("head")[0].appendChild(fileref)
}

Or with jQuery:

function loadCSSFile (filename) {
    $("<link>", {
         rel: "stylesheet"
       , type: "text/css"
       , href: filename
    }).appendTo("head");
}

Then you will do:

if (window.top!=window.self) { 
    loadCSSFile("http://www.website.com/content/themes/theme/style.css");
} else { 
    loadCSSFile("http://www.website.com/content/themes/theme/iframe.css");
}
Sign up to request clarification or add additional context in comments.

4 Comments

if this was on wordpress website, where do i put the JavaScript of jQuery any ideas? I'm guessing the if statement will be on the page header?
@user3348182 It really doesn't matter in this case. Just wrap the code using <script>...</script> tags and it should work.
Im afraid i can't get this working it will be simple I'm just probably doing something wrong i need to load this in <HEAD> part of the document its not loading any of the CSS files not even the original :/ any help?
You need to do the top one for anyone who needs it in the future! FANTASTIC! Thank you!

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.