4

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.

  • 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. – Shoaib Nawaz Jun 27 '12 at 15:23
  • @Shuaib Nawaz I edited above based on your question and comment. – Tom Jun 27 '12 at 15:59
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?

  • 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 Jun 27 '12 at 16:32
1

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

- core/drupal

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.