8

Using javascript in Drupal 6, is there a way to detect whether the present page is the front page? I have a custom Drupal 6 module that outputs different javascript codes based on whether the present page is front page or not. It adds a single javascript file to drupal, so I need to put the check in the javascript.

The site will be served through Varnish cache, so I don't want to add the check in PHP, though I know that would have been way easier with drupal_is_front_page().

Is there any Drupal-native way to do this? jQuery based suggestions will be highly appreciated.

Thanks.

migrated from stackoverflow.com Jul 10 '11 at 23:15

This question came from our site for professional and enthusiast programmers.

19

Generally Drupal adds a "front" class to the body element of whatever page is acting as the front page. If your theme's page.tpl.php is printing the body classes (i.e <body class="<?php echo $classes; ?>"), which it most likely is (it should), then your javascript can check if the body element has the "front" class.

So, assuming jQuery (because Drupal includes it) then you'd do:

if ($('body').hasClass('front')) { // do stuff...}
  • Thanks @Alexander! Got it working. I was thinking too complexly :) Thought there might be some native Drupal functions when I could simply use jQuery's selectors. – kaustavdm Sep 18 '11 at 14:40
0

Print the result of drupal_is_front_page() into a javascript variable, using json formatting if necessary. And just write a javascript function to check it when you need it.

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.