0

I checked many pages in Drupal Answers, which didn't have the answer of the following question:

Consider a case that I need javascript to be enabled for visual effects in drupal design.

How can I stop my website and show a message , if javascript is not enabled in user's browser?

Stop means to block the access of user to the website and showing a message like:

Please enable JS in your browser!

(I use Drupal 7.x)

6

It is not possible, not in the way you think of. Workflow is:

  1. Server gets HTTP request.
  2. Drupal process that request and returns HTML.
  3. Browser runs JavaScript, or does not.

As you can see, server never knows if JS is there. If JS is on, you can send AJAX request, but if it's not, you can't do anything at this point.

You have three options:

  1. Graceful degradation - this is what I would recommend. Use modules and write code that will simply not fail if JS is not there. It may look differently, that's OK, but should look good and work all right. I recommend these articles: Fault tolerance, Unobtrusive JavaScript

  2. Use HTML <noscript> tag. It will not hide the rest of your site, but it will tell your users what is happening. This method can be used together with 1st one. Related Q&A: How to Add noscript in head tag (Thanks Wtower for link)

  3. Output a page with "Please enable JS in your browser!" message and immediately load real content with AJAX. That's the only way I can think of to make your site work like you want it to, but it is so wrong, on so many levels... Remember, some bots may be unable to index your page if you don't have JS enabled, your users with JS on would see message while waiting for real content anyway, and so on. And while Google can read JS-generated content now, Bing and other search engines apparently still cannot.

|improve this answer|||||
2

I just wrote the following code by reading the answer of Mołot for the question, as follows:

function MYMODULE_init(){

  if(current_path()!="enable-javascript"){

    $target = url('enable-javascript', array('absolute' => TRUE));
    $meta = array(
      '#theme' => 'html_tag',
      '#tag' => 'meta',
      '#attributes' => array(
        'http-equiv' => 'refresh',
        'content' => "2; url=$target"
        )
      );
    $noscript = array(
      '#theme' => 'html_tag',
      '#tag' => 'noscript',
      '#value' => drupal_render($meta)
      );
    drupal_add_html_head($noscript, 'noscript');
  }
}

Just put the code inside a module, and clear the cache. The reason for the condition is to prevent the meta tag for the /enable-javascript path itself. It means to stop rechecking and refreshing for this path every 2 seconds (according to the setting in the code).

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.