The Issue
When using the Google Custom Search Engine control and executing a default query it throws a JavaScript error “Error: ‘this.zd’ is null or not an object”.

The Problem Code
google.load('search', '1'); function OnLoad() { // Create a custom search control that uses a CSE restricted to code.google.com var customSearchControl = new google.search.CustomSearchControl('012157912978810372049:-cv6ao3zqua'); // run a query customSearchControl.execute('ajax api'); // Draw the control in content div customSearchControl.draw('content'); } google.setOnLoadCallback(OnLoad); |
The Solution
Sometimes the most obvious mistakes are the hardest to find. After several Google searches it started to seem like no one else ever had this problem. Threre was no obvious connection between this error and the Google CSE. After staring at the examples here http://code.google.com/apis/ajax/playground/#custom_search_control for a while the answer became obvious.
Before executing the default query the control must first be drawn. The last two commands in the OnLoad method in the above code need to be flipped. Here is the correct code:
google.load('search', '1'); function OnLoad() { // Create a custom search control that uses a CSE restricted to code.google.com var customSearchControl = new google.search.CustomSearchControl('012157912978810372049:-cv6ao3zqua'); // Draw the control in content div customSearchControl.draw('content'); // run a query (ONLY AFTER DRAWING THE CONTROL) customSearchControl.execute('ajax api'); } google.setOnLoadCallback(OnLoad); |