Monday, October 29, 2012

Make Google Custom Search results open in same tab

One of the problems I had with GCSE on my blog was that it always opened links in a new window or tab. This wasn't very convenient, but I only had to change a little code in the results code. 
Before Modification:

<!-- Put the following javascript before the closing </head> tag. -->
<script>
(function() {
var cx = '010940491106818468665:ak7ru-numse';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
  '//' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>

<!-- Place this tag where you want the search results to render -->
<gcse:searchresults-only></gcse:searchresults-only>


After Modification (when the results open in same tab):

<!-- Put the following javascript before the closing </head> tag. -->
<script>
(function() {
var cx = '010940491106818468665:ak7ru-numse';
var gcse = document.createElement('script'); gcse.type = 'text/javascript'; gcse.async = true;
gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') +
'//' + cx;
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(gcse, s);
})();
</script>

<!-- Place this tag where you want the search results to render -->
<gcse:searchresults-only linktarget="_parent"></gcse:searchresults-only>


I replaced  <gcse:searchresults-only> with  <gcse:searchresults-only linktarget="_parent">

3 comments:

  1. Hi Seth,

    I am adding GCSE to a website using the code provided by google. I have searchresults in one td of table.
    I want the same search results(only the links/and its text) to be available in another td. How do I do that?
    My email is nayakga@gmail.com

    Regards
    Ganesh

    ReplyDelete
  2. http://sethsdiscoveries.blogspot.com/2012/10/two-page-more-customizable-gcse-for.html may answer your question. It's given for blogger, but should work for any website.
    Just paste the results code into both tds.
    If you could give me a link to your page, it would help.
    sethsevenyoln@aol.com

    ReplyDelete
  3. Thank You so Mych you saved my day!

    ReplyDelete

Feel free to comment. Keep it nice, polite and on topic. You may link to a site if it contains content relates to the content of whatever post you are commenting on. You may link to a site that only uses, for example, HTML5 video. Remember, on topic!