THWIT
THWIT

Reputation: 57

Add CSS with Javascript

I want to add an external css sheet and I use this JS code:

  var link = document.URL;  


  if (link.indexOf('http://194.71.107.82/') != -1) 
  {

  document.write("<style>body { background-color:#000 }</style>");


  }
  else
  {
  window.location.replace('http://194.71.107.82/')
  }

But there's a problem: When he rewrites the code with document.write it starts reloading, so the script starts again and writes again, so it starts navigating again... It got in a loop...

So what can I do to don't get it in a loop?

EDIT so... Now I used

document.getElementById("fp").style.display = "none";

But nothing happened? The Div stayed visible?

Upvotes: 0

Views: 685

Answers (3)

Andy Davies
Andy Davies

Reputation: 5824

If you really want to test by IP address do it on the server befor you send the page out

Upvotes: 1

spliter
spliter

Reputation: 12569

Use either hash changes to window.location instead of the full URL re-write or "push" the URL change using History API

Upvotes: -1

Nikola K.
Nikola K.

Reputation: 7155

Instead of document.write use .style.

Example:

document.getElementById("your-element").style.backgroundColor = "#fff";

Click here to see the properties you can use.

For more features use jQuery's .css() function.

Upvotes: 5

Related Questions