Disable website if js disabled

how can I prevent access to the website if the user has js disabled?

You check server side.

one option is to hide the <body> by default using css and then use javascript to unhide the <body>.

Sorry, How do I check server side?

Hi, how would I use jquery to unhide the body?

Also, using css is it possible for the user to simply change the css and get access to the content?

Why would you want to use jquery for something so simple to do with vanilla javascript.

body {
    display: none;
}

window.onload=function(){
     document.body.style.display='block';
}

Yes. If they want to. Everything you do client side can be bypassed by the user.

Why do you want to hide the content of your side?

yes it is. So if that’s an issue for you, then hopefully guido2004 will come back to show you how to do it server side.

Thank you for your responses. I have managed to disable access however is there an alternative to CSS I don’t want the user editing the css and viewing the content if the js is disabled.

I don’t quite understand what you’re trying to achieve?

  1. Are you hiding the content with JS, and when a user disables JS that hiding doesn’t work?
  2. Or do you want to hide the content ONLY if the user has JS disabled? If so, why?
  3. Something else?

If it’s 1), then you could check server side (using PHP or any other server side language) to show or not show any content you want in any given circumstance.
If it’s 2), then you can’t do anything server side, because the server can’t know if the client has JS enabled or not.

Another option:

In the <head> use javascript to redirect to a page for javascript enabled browers

If javascript is disabled, then you will stay on the original page and you then display appropriate content for javascript disabled browsers.

You don’t need to bother the server at all :slight_smile:

Hi thank you for your responses. I’m trying to completely restrict content for users that have js disabled.

Is this possible?

<? $test = “hello2” ?>
<noscript>
<? $test = “hello” ?>
</noscript>
<? echo $test; ?>

I tested it, does not seem to work. Maybe i’m missing something.

Then why is a redirect as described in post 12 not a possible solution, unless I am misunderstanding what you want to do.

One way to do it is to check if a cookie has been set using some cookie handling functions and then reload the page, which the server can then use to provide the appropriate content.


if (location.search === '') {
var hasjs = readCookie('hasjs');
if (hasjs !== 'yes') {
    createCookie('hasjs', 'yes');
    location.reload();
}

The php script can then check to see if that cookie value exists, and give the appropriate content if it has.

and if cookies are disabled in the browser?

You could instead post a form back to the server.

Even though the OP’s request is not normally how things are done, how would you go about things?

If you’re asking me, I’ve already answered that in this thread.

You could put the entire content of the page in a document.write() function, and then it will only appear for people with Javascript running. But why would you want to do that?

You can use ajax. If JS is enabled, the request will process, if not, the user sees nothing.