9 April 2025

This code creates a URL object from a given relative URL and extracts either the q query parameter (if present) or the pathname. It then checks if the URL is local and starts with a specific path (my-path), exiting early if either condition is false.

Source code viewer
  1. var url_object = new URL(Drupal.absoluteUrl(url));
  2. var url_query = url_object.searchParams.has('q') ? '/' + url_object.searchParams.get('q') : url_object.pathname;
  3. if (!Drupal.urlIsLocal(url) || !url_query.startsWith(Drupal.settings.basePath + Drupal.settings.pathPrefix + 'my-path')) {
  4. return;
  5. }
Programming Language: Javascript