ajaxify content via a callback function

I am trying to ajaxify my site. I want the “Main page content” to be passed via an ajax-command to a custom js-function.

To accomplish this I would do the following:

  1. I need to register a path for my ajax requests

code:

$items['_custom_ajax_'] = array(   'title' => 'AHAH callback',   'page callback' => 'ajax_form_callback',   'delivery callback' => '_custom_ajax_deliver',   'access callback' => TRUE,   'theme callback' => 'ajax_base_page_theme',   'type' => MENU_CALLBACK, ); 
  1. use a custom delivery callback function _custom_ajax_deliver which is the copy of the ajax_deliver()

original code:

function ajax_deliver($page_callback_result) {   // Browsers do not allow JavaScript to read the contents of a user's local   // files. To work around that, the jQuery Form plugin submits forms containing   // a file input element to an IFRAME, instead of using XHR. Browsers do not   // normally expect JSON strings as content within an IFRAME, so the response   // must be customized accordingly.   // @see http://malsup.com/jquery/form/#file-upload   // @see Drupal.ajax.prototype.beforeSend()   $iframe_upload = !empty($_POST['ajax_iframe_upload']);    // Emit a Content-Type HTTP header if none has been added by the page callback   // or by a wrapping delivery callback.   if (is_null(drupal_get_http_header('Content-Type'))) {     if (!$iframe_upload) {       // Standard JSON can be returned to a browser's XHR object, and to       // non-browser user agents.       // @see http://www.ietf.org/rfc/rfc4627.txt?number=4627       drupal_add_http_header('Content-Type', 'application/json; charset=utf-8');     }     else {       // Browser IFRAMEs expect HTML. With most other content types, Internet       // Explorer presents the user with a download prompt.       drupal_add_http_header('Content-Type', 'text/html; charset=utf-8');     }   }    // Print the response.   $commands = ajax_prepare_response($page_callback_result);   $json = ajax_render($commands);   if (!$iframe_upload) {     // Standard JSON can be returned to a browser's XHR object, and to     // non-browser user agents.     print $json;   }   else {     // Browser IFRAMEs expect HTML. Browser extensions, such as Linkification     // and Skype's Browser Highlighter, convert URLs, phone numbers, etc. into     // links. This corrupts the JSON response. Protect the integrity of the     // JSON data by making it the value of a textarea.     // @see http://malsup.com/jquery/form/#file-upload     // @see http://drupal.org/node/1009382     print '<textarea>' . $json . '</textarea>';   }    // Perform end-of-request tasks.   ajax_footer(); } 
  1. create an custom function which do the ajax request like Drupal.ajax

  2. bind the ajax request to some selector e.g. a.ajaxify

  3. add a custom ajax-command

Im not sure if this is the right way, or if I can extend the core easier somehow. Basicly my approach is simmilar to the module Ajaxify Drupal with JQuery Ajax

Better Ideas?

Update: one more thing – I need an event to be triggered everytime an ajax-link is clicked.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

ajaxify content via a callback function

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.