谷歌搜索框javascript

时间:2012-08-09 12:30:12

标签: javascript

我已经创建了一个谷歌搜索框java脚本,我已经在iWeb中应用它,它看起来很好,但是当我进行搜索时它没有响应。

以下是代码:

 <!-- Put the following javascript before the closing </head> tag. -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script>
(function() {
var cx = '012476238773441236228:ivxtfsk4-uc';
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>

<!-- Place this tag where you want both of the search box and the search results to render   -->
<gcse:search></gcse:search>

1 个答案:

答案 0 :(得分:0)

您提供的脚本有效(如this jsFiddle所示)。您可以安全地删除<!DocType元标记。

e.g。

<head>
  <!-- Current head content here -->
  <script type="text/javascript">
    (function() {
      var cx = '012476238773441236228:ivxtfsk4-uc';
      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>

在你的身体里,无论你想要盒子出现在哪里:

<gcse:search></gcse:search>​

也许您可以使用当前的代码创建一个jsFiddle用于调试帮助吗?