This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: Question marks stop ‘Scroll to first keyword match’ from working

Home Forums Product Support Forums Ajax Search Pro for WordPress Support Question marks stop ‘Scroll to first keyword match’ from working Reply To: Question marks stop ‘Scroll to first keyword match’ from working

#57312
Ernest MarcinkoErnest Marcinko
Keymaster

Hi Stuart,

I see. I am not sure if this should be patched, as I feel this should be intentional for most cases, but I will definitely consider it.

You can actually fix this through a code snippet, but as you noticed this is on the client side, not on the server side. This should do the trick:

add_action('wp_footer', function () {
	?>
	<script>
	window.addEventListener("load", () => {
		document.querySelectorAll(".asp_main_container").forEach((el) => {
			el.addEventListener("asp_results_show", (event) => {
				// event.detail carries the arguments
				const [id, instance] = event.detail;
				document.querySelectorAll(".asp_r_" + id + '_' + instance + ' .asp_res_url').forEach((el)=>{
					const url = new URL( el.getAttribute('href') );
					let phrase = url.searchParams.get('asp_highlight');
					if ( phrase !== null ) {
						phrase = phrase.replace(/[!?]/g, '');
						url.searchParams.set('asp_highlight', phrase);
						el.setAttribute('href', url.href);
					}
				});
			});
		});
	});
	</script>
	<?php
});