Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want google search to search my website. I have a textbox to add text and a button to submit. So when I click submit i want it to search through my website for the words.

here is the code google gave me

XML
<script>
  (function() {
    var cx = '012117305473792254352:kosbw5yycts';
    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>
<gcse:search></gcse:search>
Posted
Comments
André Kraak 21-Mar-14 14:24pm    
PythonProgrammer 21-Mar-14 14:31pm    
That is what I am using and they gave me that code above so how do i use that code to search on my webiste?

1 solution

Add the <script> block in the <head> section of your page. Then instead of your text box and submit button, write the line:
HTML
<gcse:search></gcse:search>

This element will be automatically replaced with a text box and search button at run time. Here is the complete HTML that shows how to use google search:

HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head  runat="server">
    <title></title>
    <script>
        (function () {
            var cx = '012117305473792254352:kosbw5yycts';
            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>

</head>
<body>
    <form id="form1"  runat="server">
    <div>
        <gcse:search></gcse:search>
    </div>
    </form>
</body>
</html>
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900