2

I'm building a simple module that shows a jQuery UI dialog. I have some HTML in my code but I only want to show this to users that have javascript enabled.

to give you an example here are the contents of my .module file:

function popover_init() {
    drupal_add_library('system', 'ui.dialog');
    drupal_add_js(drupal_get_path('module','popover') . '/popover.js', 'file');
}
?>
<div id="dialog" title="D7 jQuery dialog">
    <p> bla bla bla </p>
</div>

How do I check the drupal way that javascript is enabled so I can stop my module from loading unnecessary stuff ?

0

3 Answers 3

1

You should be able to do the following that will hide the div from non-JS users:

function popover_init() {

  $js = "jQuery(document).ready(function(){";
  $js = "jQuery('#dialog').css('display', 'block');";
  $js = "});";

  drupal_add_library('system', 'ui.dialog');
  drupal_add_js(drupal_get_path('module','popover') . '/popover.js', 'file');
  drupal_add_js($js, 'inline'); 
}

<div id="dialog" title="D7 jQuery dialog" style="display:none">
    <p> bla bla bla </p>
</div>

Basically, your dialog div starts out hidden (with the inline CSS), and is then unhidden with jQuery.

If the user doesn't have JavaScript enabled, then the block will not be made visible!

I'm also assuming that this part:

<div id="dialog" title="D7 jQuery dialog">
  <p> bla bla bla </p>
</div>

is in a template file, and not just sitting outside a function in your module.

1
  • that's what i was thinking off guess this is the solution if there isn't a drupal_check_js_enabled() kind of function.
    – FLY
    Apr 12, 2012 at 7:30
1

I don't think there is a way. Because your module runs on the server and it cannot interact with the client's browser in any way.

Here's an answer on stackoverflow: Check if JavaScript is enabled with PHP

2
  • I thought there was a build in drupal why for checking if javascript was enabled...could be mistaken though..
    – FLY
    Apr 11, 2012 at 15:45
  • There are several ways of doing this. Check out Chapabu's answer.
    – asiby
    Nov 25, 2017 at 22:59
0

I was thinking you could do something in javascript that calls a php page on the server that sets a session variable and then use that as a flag, and, sure enough, the code to do that is in the stackoverflow article cited by @karthik...

<html>
  <head>
    <script type="text/javascript" src="jquery1.4.4.js"></script>
    <script type="text/javascript">
    $(document).ready(function(){
       $.get("myPage.php");
     });
    </script>
  </head>
</html>

and

<?php
  // not needed for drupal // session_start();
  $_SESSION['js'] = true;
?>

So in drupal, you could add the js in the above header and then set up a menu() item and callback that does the php and then in your module you would check on the session variable being set.

Pretty convoluted and probably more time consuming than just putting a

<noscript>
  Our site really works much better with javascript enabled....
</noscript>

in your page.tpl though, imho...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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