0

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?

1
  • Use Yepnope.JS yepnopejs.com Commented Jul 1, 2012 at 18:33

3 Answers 3

5

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.

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

4 Comments

But when I try: document.getElementById("fp").style.display = "none"; Strange enough nothing happened?
The display property is not supported by Javascript. See the link i provided. In order to use all properties, use jQuery and .css() (see the second link).
Ok thank you, how can I use jQuery in JS or is that not possible?
Oh, that's very easy. Download the include file from jquery.com, include it and you're done :) jQuery is JavaScript library.
1

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

Comments

-1

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

2 Comments

Hashbangs are ruining the Internet since everything after hash isn't considered part of the URL, this is terrible advice.
@Esko, I hope you don't mind using History API, do you? Then, the advise is not as terrible as it seems, I would say. But, yeah, I hear 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.