How to load Google Custom Search results dynamically using ajax

3 replies·opened Aug 31, 2020
M
michaelgr42Aug 31, 2020

I have an audio player on my site, so I cannot page refresh.
I am using ajax to dynamically load the content.

I have set up the google custom search, however, I cannot figure out how I would go about loading the results without a page refresh.
My code for dynamically loading content works like this.

When a link(href) is clicked, I trigger a hash change, then return false to prevent the default behaviour.

				// Trigger Hash Change
				let page = $(this).attr("href");
				window.location.hash = page;

Then I have another function that detects a hash change and will load the appropriate content.

			$(window).bind('hashchange', function () {
	
				let newHash = window.location.hash.substring(1);
									
				$("#main-content").load(newHash + " #ajax-content", function (responseTxt, statusTxt, xhr) {

				}).hide().fadeIn();
			});

The content of my search page is set up like this

<div id="main-content">
	<div id="ajax-content" title="Search">
        
                <!-- Content
                ============================================= -->
                <section id="content">
                    <div class="content-wrap">
                        <div class="container clearfix">
        
                            <gcse:searchresults-only></gcse:searchresults-only>
        
                        </div>
                    </div>
                </section><!-- #content end -->
	</div>
</div>

then for the search form I have not changed anything

<form class="top-search-form" action="search.html" method="get">
	<input type="text" name="q" class="form-control" value=""
	placeholder="Type & Hit Enter.." autocomplete="off">
</form>
S
SemiColonWebSTAFFSep 1, 2020

The Google Custom Search JS Settings are required on the Page:

<script>
	(function() {
		var cx = '006140480002229796823:3f-_bji0d1w';
		var gcse = document.createElement('script');
		gcse.type = 'text/javascript';
		gcse.async = true;
		gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
			'//www.google.com/cse/cse.js?cx=' + cx;
		var s = document.getElementsByTagName('script')[0];
		s.parentNode.insertBefore(gcse, s);
	})();
</script>

Reference: http://docs.semicolonweb.com/docs/widgets/google-custom-search-setup/ .

We Apologize about the Inconveniences, however, we currently do not provide Customization related Support according to the Item Support Policy: https://themeforest.net/page/item_support_policy . You will need to manually code this or Hire a Freelancer to help you out.

M
michaelgr42Sep 1, 2020

I have already included the script, the search works. I was just enquiring if there is a way to do it without refreshing the page?

S
SemiColonWebSTAFFSep 1, 2020

We understand. However, unfortunately, this is currently not support out of the box.

Let us know if we can help you with anything else.

Have the same question, or something new?

Sign in to the Canvas dashboard to reply or open your own topic. Canvas owners get direct help from the SemiColonWeb team.

Reply on the dashboard