5

For Drupal 7, a jQuery will not load for "anonymous" users on any template pages. But when I log in as an administrator it loads. It's similar to this issue: http://drupal.org/node/802432, but nothing I tried has worked. The only way I could get the jQuery to load was by giving the user access to the administration menu, which I do not want to do.

I have the following code in the preprocess_node function of the template.php file.

if ($variables['view_mode'] == 'full') {
$node =& $variables['node'];
if ($node->type == 'page') {
  $path = drupal_get_path('theme', 'responsive');
   drupal_add_js($path . '/js/jquery.snap.js', array('type' => 'file', 'scope' 
   => 'header'));
   drupal_add_js($path . '/js/jquery.snap2.js', array('type' => 'file', 'scope'
   =>'header'));
  }
}

Here is the original JS code

<script type="text/javascript">
document.write(unescape("%3Cscript src='" 
+((document.location.protocol=="https:")?"https://snapabug.appspot.com":
"http: //www.snapengage.com") + "/snapabug.js' type='text/javascript
'%3E%3C /script%3E"));
</script>
<script type="text/javascript">
SnapABug.addButton("77777-7777","0","50%", true);
</script>

I put the code in two separate JS files (couldn't get it to work in a single file)

jquery.snap.js

(function($) {
document.write(unescape("%3Cscript src='" 
+  ((document.location.protocol=="https:")?"https://snapabug.appspot.com":
"http: //www.snapengage.com") + "/snapabug.js' type='text/javascript
'%3E%3C /script%3E"));
})(jQuery);

jquery.snap2.js

(function($) {
SnapABug.addButton("77777-7777","0","50%", true);
})(jQuery);

When I look at the HTML source I see an error for jquery.snap2.js: "Uncaught Reference Error: SnapBug is not defined." The result is the snapabug.js is not loading. This error does not occur on the administrative side and the snapabug.js does load.

2
  • Does your intended file (jquery.snap.js) included in the page. Check your html source, if it is only with jQuery or other js files as well. Commented Jun 27, 2012 at 15:23
  • @Shuaib Nawaz I edited above based on your question and comment.
    – Tom
    Commented Jun 27, 2012 at 15:59

2 Answers 2

2

Drupal does not load jQuery if there are no dependency on it.

When drupal_add_js() is used once any where, Drupal includes jQuery automatically.

In case of user session, admin menu and overlay are dependent on jQuery so it is added automatically. So i advise you to try your drupal_add_js() code in hook_init just for try. I am not confirmed if it does not have any effect if it is called in hook_preprocess.

Please also check if you hook_node_preprocess is working at all?

1
  • I did find the source of the error. There was some old code fragment in the in the template.php that I thought I had gotten rid of. Not sure why it worked on the administrative side with this left-over code in there. Not sure if I should leave this question or delete. Someone more experienced on here please advise. Though, Shuaib did help me find the source of the problem so I would like to give credit. Thanks @ShuaibNawaz.
    – Tom
    Commented Jun 27, 2012 at 16:32
2

And in Drupal 8 just add Drupal js as a dependency to your theme library

- core/drupal
3
  • does this still work in later versions of 8 and Drupal 9? In 9 if I use your answer I get this error when I rund drush updb : (Currently using Unresolved dependency core (Missing) ) I've also noticed some modules, like Views, use a colon in their dependencies - is the colon the new standard, instead of the slash? e.g. drupal:filter Commented Oct 21, 2020 at 15:42
  • acrually this is fine - no error if this dependency line is added to libraries.yml of the module. NOT info.yml thank you! Commented Oct 21, 2020 at 16:06
  • @therobyouknow glad to hear it helped you after 3 years !!! :) ^_^
    – Yuseferi
    Commented Oct 21, 2020 at 18:09

Your Answer

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

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