0

First a confession: I did not write the JavaScript reproduced below. Unfortunately, I am a rookie JavaScripter, and this script was scavenged from another project and slightly adapted by me.

The script is using a jQuery function to flip the CSS of a webpage to expose an "active" category tabs list item and hide the rest (i.e. it lets the user navigate between tabs on a single Drupal pages without suffering the delays of reloading the page from the server).

It works as intended. Here is the code (rbg.js);

(function ($) {

Drupal.behaviors.rbg = {};

Drupal.behaviors.rbg.attach = function(context) {
  // set default Category tab if one is set in URL anchor
  var anchor = window.location.hash;
  if (anchor) {
    $('.rbg-category-tabs a.active').removeClass('active');
    $('.rbg-category-tabs li a[href=' + anchor + ']').addClass('active');
    $('.panel').hide();
    $(anchor).show();
  }
  // show the selected category panel
  $('.rbg-category-tabs a').click(function() {
    $this = $(this);
    $('.panel').hide();
    $('.rbg-category-tabs a.active').removeClass('active');
    $this.addClass('active').blur();
    var panel = $this.attr('href');
    $(panel).fadeIn(330);
    $('input[name="category"]').val(panel);
    return false;
  });

};
}(jQuery));

I use this to add the Javascript to my Drupal site:

// Add javascript.
drupal_add_js(drupal_get_path('module', 'rbg') . '/rbg.js');

I want to know the value of the variable anchor on the next Drupal page load, I am simply unable to figure out how to do it. I've tried to read up on Drupal's Ajax framework, but I am unable to figure it out on my own.

  • I don't see any value in trying to use a Drupal-specific method/flow for this, jQuery.ajax would make more sense for what you've described. You need to make an AJAX request containing the value you want to save (as POST data would be best), save that value in the session in PHP, then it'll be available in the session on the next page load (assuming that the session is started for both the AJAX and the subsequent request) – Clive Aug 15 '18 at 15:38
  • Does next Drupal page load not have an anchor (#) in the url? – No Sssweat Aug 16 '18 at 12:21
  • @NoSssweat: "Does next Drupal page load not have an anchor (#) in the url?" No. The user clicks an URL to load the next page. The URL is generated by me (by PHP - on the server side). I want this URL to have an anchor. What motivates this question is to learn how get the value of the JavaScript variable "anchor" so I can generate an URL with the correct anchor. – Free Radical Aug 16 '18 at 12:56

Your Answer

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

Browse other questions tagged or ask your own question.