开发者

integrate search into header/masterpage of asp.net MVC site

开发者 https://www.devze.com 2023-02-11 08:36 出处:网络
I want to add search for my website. Im thinking about using google custom search... what I need is to have a search box in the header of the master page, and then a separate search results page wh开发

I want to add search for my website. Im thinking about using google custom search... what I need is to have a search box in the header of the master page, and then a separate search results page wh开发者_开发百科en a user searches.

how can i implement this?, as the code snippet given by google seems to be for the results page I guess? also can i integrate this into a site that is still in development?


Implement the code on the results page as suggested by Google. Then in your master page add the following

<form id="searchForm" method="get" action="/url-to-your-search-page/" >
  <input id="search" name="search" class="Search" type="text" />
  <input type="submit" value="search" />
</form>

The Google custom search code on the search page will take care of the rest.

Update

You can use the following JavaScript at the bottom of your search results page to extract the search query string and execute a Google search

<script type="text/javascript">
    google.load('search', '1');
    function OnLoad() {
        var s = window.location.search;
        if (s.indexOf('search=') >= 0) {
            s = s.substring(s.indexOf('search=') + 7);
            if (s.indexOf('&') >= 0) {
                s = s.substring(0, s.indexOf('&'));
            }
            s = decodeURIComponent(s.replace('+', ' '));
        }
        else {
            s = "";
        }
        var customSearchControl = new google.search.CustomSearchControl('your-custom-search-id');
        customSearchControl.draw('content');
        customSearchControl.execute(s);
    }
    google.setOnLoadCallback(OnLoad);
</script>

Don't forget to include your script reference in the header

<script src="http://www.google.com/jsapi"></script>


I think choosing the iframe option when creating the Google custom search is the way to do it. it separates ou the code into a searchbox, and a results page

0

精彩评论

暂无评论...
验证码 换一张
取 消