I'm trying to have multiple search field on the same page with Google Custom Search (GCS) like this :
<script>
(function() {
var cx = 'user_id:field_id1';
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>
<script>
(function() {
var cx = 'user_id:field_id2';
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>
Unfortunatly, It does not work. The search is made with the same cx
for every field. When It do the ajax request on this address : https://www.googleapis.com/customsearch/v1element... there is this value : &cx=user_id:field_id1
and the value is the same for both fields.
What is the solution?
I already seen this question : Multiple Google CSE (Custom Search Engine) Boxes on Same Page, but it seems to be on another version.
Try using iFrames
:
<html>
<head>
<title></title>
<style type="text/css">
/* Layout Style */
</style>
</head>
<body>
<iframe src="gcse1.html"></iframe>
<iframe src="gcse2.html"></iframe>
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
<script>
(function() {
var cx = 'user_id:field_id1';
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>
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
<script>
(function() {
var cx = 'user_id:field_id2';
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>
</body>
</html>