Ajax Search Pro Knowledge Base
Buy Ajax Search Pro!DocumentationGet SupportDevelopment
  • Knowledge Base
  • Safe Coding Guideline
  • Hooks
    • Filters
      • Query & Output
        • asp_query_args
        • asp_query_{type}
        • asp_cached_content
        • asp_pre_get_front_filters
        • asp_before_ajax_output
        • asp_shortcode_output
        • asp_print_search_query
      • Keyword Suggestions
      • Search Results
        • asp_suggested_phrases
        • asp_results
        • asp_cpt_results
        • asp_buddyp_results
        • asp_attachment_results
        • asp_comment_results
        • asp_blog_results
        • asp_terms_results
        • asp_peepso_group_results
        • asp_peepso_activities_results
        • asp_only_keyword_results
        • asp_only_non_keyword_results
        • asp_result_groups
      • CSS & JS
        • asp_load_js
        • asp_load_css
        • asp_load_css_js
      • Template & Output
        • asp_icl_t
      • Index Table Related
        • asp_index_on_save_stop
        • asp_indexing_keywords
        • asp_indexing_string_pre_process
        • asp_indexing_string_post_process
        • asp_post_content_before_tokenize_clear
        • asp_post_content_before_tokenize
        • asp_post_excerpt_before_tokenize
        • asp_post_title_before_tokenize
        • asp_file_contents_before_tokenize
        • asp_post_permalink_before_tokenize
        • asp_index_terms
        • asp_post_custom_field_before_tokenize
        • asp_index_cf_contents_before_tokenize
        • asp_index_before_shortcode_execution
        • asp_index_after_shortcode_execution
    • Templating
      • Filter layouts Templating
      • Result Templating
    • Constants
  • Frontend Filters
    • Taxonomy Filters
      • Restricting results to the same category as the current post object
      • How to automatically check/select filter values based on the archive page?
    • Frontend filters API
  • Tips & Miscellaneous
    • Divi
      • Divi Blogs Live Search and Filter
    • Jet Engine
      • Jet Engine Listing Grid Live Search and Filter
      • Searching Jet Engine Custom Meta Storage fields
    • Tutorials
      • PDF results thumbnails
      • Demo setup: Staff search and Filter
      • Demo Setup: WooCommerce Search
      • Demo setup: WooCommerce Shop Search and Filter
      • Demo Setup: Events Search – Events Manager
      • Demo Setup: Events Search – The Events Calendar
      • Compact ‘pop-out’ search bar placement on specific pages only
      • Index Table – Indexing ACF repeater field titles and contents
      • Change Suggested Phrases conditionally
      • How to add shortcode to the results content?
      • How to add variables to the “redirect to url” or the “show more url”?
      • Indexing Shortcodes within custom field contents
    • Post Types
      • Index Table - Indexing child post contents to parent
      • Limit results to specific post IDs only
      • Filter posts (or CPT) which user can’t access
      • Restricting results by user Groups using the Groups plugin by itthinx
      • Limiting results to specific posts by parent ID
      • Limit results to current page children
      • Excluding posts or CPT by parent ID(s)
      • Searching posts, pages (or any CPT) by specified keywords only, nothing else
      • Showing the post type name in result title or content
      • Searching within given categories/taxonomy terms only
      • Search only in the same category as the current post or page (single page)
      • Search only within the current category (or any taxonomy) archive
      • Filtering pages by page template
    • Taxonomy Terms
      • Displaying taxonomy name in taxonomy term results
      • Limiting taxonomy term results to specific term IDs only
    • WooCommerce
      • Displaying On Sale products only in WooCommerce
      • Ordering product by stock status
      • Making a product in-stock & out of stock filter
      • Add to cart button for Vertical and Horizontal results
      • Get formatted price in result title or in content
      • Showing products in-stock only
      • Showing in-stock and backorder products only
      • Excluding hidden catalog products
      • Showing featured products first
      • How to search products & product SKU?
      • How to search Products by variation SKUs?
      • Search product attributes
      • How to search users by city, state, zip code… ?
    • Compatibility
      • WPML Compatibility issues and fixes
    • Files & Media
      • Searching Image EXIF metadata
    • Other
      • Restrict results from the current author archive page
      • Presetting search options via a custom URL
      • Matomo analytics tracking integration
      • Replace search keywords (whole words)
      • Replace or remove characters from search phrase
      • Adding spaces in search phrase between alphabetics and numbers
      • How to change the results URL to something else?
      • Numbering the results
      • How to use the search without the live ajax feature, as a regular search?
      • Empty search input field on the search results page
      • Singular and Plural keywords index
  • Other
    • SearchQuery
    • REST API
    • Building a custom REST API
    • Javascript API
    • Javascript Hooks
      • asp_redirect_url
      • asp_search_data
      • asp_live_load_html
      • asp_search_html
      • asp_compact_width
    • Theme Functions
Powered by GitBook
On this page
  • Events list
  • Common arguments
  • asp_init_search_bar
  • asp_settings_show
  • asp_settings_hide
  • asp_results_show
  • asp_results_hide
  • asp_elementor_results
  • asp_search_start
  • asp_search_end
  • Sample implementation to the functions.php file in theme/child theme directory
  • Triggering Events/Functions
  • Function arguments
  • Functions list
  • searchFor
  • searchRedirect
  • toggleSettings
  • closeResults
  • getStateURL
  • Examples
  1. Other

Javascript API

The javascript API consists of two parts, one is the events triggered by the plugin, and methods, which you can use to trigger specific events.

Events list

The events are fired on the main search container element which you can target with ".asp_main_container" class selector. For example attaching an event handler before the search starts:

window.addEventListener("load", () => {
	document.querySelectorAll(".asp_main_container").forEach((el) => {
		el.addEventListener("asp_search_end", (event) => {
			// event.detail carries the arguments
			const [id, instance, phrase] = event.detail;
			console.log(id, instance, phrase);
		});
	});
});

Common arguments

All of the events will always pass on these three arguments (+ additional if defined):

  • event (object) - a common javascript event object, always passed in event handlers

  • id (integer) - the search ID. This is the same as the one in the shortcode.

  • instance (integer) - the instance number of the current search ID. This will help if the same search shortcode is used multiple times on the same page.

When using native javascript, the event arguments can be found in the event handlers argument, in the event.detail array. See the example above.

asp_init_search_bar

Triggers when the search search instance is initialized.

asp_settings_show

Triggers when the search settings are displayed.

asp_settings_hide

Triggers when the search settings get hidden.

asp_results_show

Triggers when the search results are displayed.

asp_results_hide

Triggers when the search results get hidden.

asp_elementor_results

Triggers when the search settings are displayed.

Additional arguments

  • element (Element) - The node object

asp_search_start

Triggers right before the search is started.

Additional arguments

  • s (string) - The search phrase

asp_search_end

Triggers right after the search is finished.

Additional arguments

  • s (string) - The search phrase

  • data (string) - HTML data returned by the server

Sample implementation to the functions.php file in theme/child theme directory

add_action('wp_footer', function () {
	?>
	<script>
		window.addEventListener("load", () => {
			document.querySelectorAll(".asp_main_container").forEach((el) => {
				el.addEventListener("asp_search_end", (event) => {
					// event.detail carries the arguments
					const [id, instance, phrase] = event.detail;
					console.log(id, instance, phrase);
				});
			});
		});
	</script>
	<?php
});

Triggering Events/Functions

To call a pre-defined event, use either version of the following function:

ASP.api( id, function_name);
ASP.api( id, instance, function_name);
ASP.api( id, function_name, args);
ASP.api( id, instance, function_name, args);

Function arguments

  • id (integer) - the search ID. This is the same as the one in the shortcode. If id=0, then the call applies to all visible search instances on the page.

  • instance (integer) - the instance number of the current search ID. This will help if the same search shortcode is used multiple times on the same page.

  • function_name - the event/function you want to trigger

  • args - arguments passed to the triggered function

Functions list

searchFor

Triggers the search for a specific search phrase.

Arguments

  • s (string) - the search phrase

Examples

// On all instances on search ID=1
ASP.api(1, "searchFor", "search phrase");

// On instance 2 of search ID=1
ASP.api(1, 2, "searchFor", "search phrase");

searchRedirect

Triggers the search redirection to the results page (as configured on the back-end) using the given search phrase.

Arguments

  • s (string) - the search phrase

Examples

// On all instances on search ID=1
ASP.api(1, "searchRedirect", "search phrase");

// On instance 2 of search ID=1
ASP.api(1, 2, "searchRedirect", "search phrase");

toggleSettings

Toggles the search settings container state to either visible or invisible.

Arguments

  • state (string) - 'show' or 'hide'

Examples

// On all instances on search ID=1
ASP.api(1, "toggleSettings", "opened");

// On instance 2 of search ID=1
ASP.api(1, 2, "toggleSettings", "closed");

closeResults

Closes the results box

Arguments

  • clear (bool) - Clears the search input, when true. Defaults to false.

Examples

// On all instances on search ID=1
ASP.api(1, "closeResults");
// On instance 2 of search ID=1, also clears the search input
ASP.api(1, 2, "closeResults", true);

getStateURL

Returns a URL for the currently viewed page, with the search settings preset to the current state. Visiting this URL will preset the search settings to that state.

Examples

// On instance 2 of search ID=1 - the instance 
ASP.api(1, 2, "closeResults", true);

Examples

Search

Search for a specific phrase on search ID 1:

ASP.api(1, "searchFor", "search phrase");

Similarly, but only on instance 1:

ASP.api(1, 1, "searchFor", "search phrase");

Search all visible instances:

ASP.api(0, "searchFor", "search phrase");

Settings

Toggle search settings box on/off:

ASP.api(1, "toggleSettings");

// ..or specific instance with the same search ID only
ASP.api(1, 2, "toggleSettings");

Explicitly show/hide the settings:

// show
ASP.api(1, "toggleSettings", "show");

// hide
ASP.api(1, "toggleSettings", "hide");

// show-hide setting of all visible instances
ASP.api(0, "toggleSettings", "show");
ASP.api(0, "toggleSettings", "hide");

Results

To hide the results window:

ASP.api(1, "closeResults");

// close all open result boxes
ASP.api(0, "closeResults");

// ..or specific instance with the same search ID only
ASP.api(1, 2, "closeResults");

Current search state URL - presetting the search filters and input to a specific state

There is a way to get the current filters and search input state on the current page, and generate a URL - which presets the search to this state.

// Get the current state of the search filters on search ID=1, and first printed instance
ASP.api(1, 1, 'getStateURL')

This returns a string like:

https://yoursite.com/?p_asid=1&p_asp_data=1&current_page_id=1041&woo_currency=USD&qtranslate_lang=0&filters_changed=0&filters_initial=1&termset%5Bcategory%5D%5B%5D=81&termset%5Bcategory%5D%5B%5D=82&termset%5Bcategory%5D%5B%5D=83&termset%5Bcategory%5D%5B%5D=98&termset%5Bcategory%5D%5B%5D=147&termset%5Bcategory%5D%5B%5D=103&termset%5Bcategory%5D%5B%5D=102&termset%5Bcategory%5D%5B%5D=100&termset%5Bcategory%5D%5B%5D=84&termset%5Bcategory%5D%5B%5D=92&termset%5Bcategory%5D%5B%5D=95&termset%5Bcategory%5D%5B%5D=94&termset%5Bcategory%5D%5B%5D=93&termset%5Bcategory%5D%5B%5D=96&termset%5Bcategory%5D%5B%5D=86&termset%5Bcategory%5D%5B%5D=87&termset%5Bcategory%5D%5B%5D=85&asp_gen%5B%5D=title&asp_gen%5B%5D=content&asp_gen%5B%5D=excerpt&post_date_from_real=31-01-2011&post_date_from=2011-01-31&post_date_to_real=23-12-2020&post_date_to=2020-12-23&customset%5B%5D=post&customset%5B%5D=page"

..visiting this URL will preset the search filters to this state.

PreviousBuilding a custom REST APINextJavascript Hooks

Last updated 10 months ago

You can learn more about this function .

here