2 // This script provides some mechanics for testing ChromeFrame
4 function onSuccess(name, id) {
5 appendStatus("Success reported!");
6 onFinished(name, id, "OK");
9 function onFailure(name, id, status) {
10 appendStatus("Failure reported: " + status);
11 onFinished(name, id, status);
15 return document.getElementById(id);
18 function getXHRObject(){
19 var XMLHTTP_PROGIDS = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP',
20 'Msxml2.XMLHTTP.4.0'];
23 http = new XMLHttpRequest();
30 for (var i = 0; i < 3; ++i) {
31 var progid = XMLHTTP_PROGIDS[i];
33 http = new ActiveXObject(progid);
43 var reportURL = "/writefile/";
45 // Optionally send the server a notification that onload was fired.
46 // To be called from within window.onload.
47 function sendOnLoadEvent() {
48 writeToServer("OnLoadEvent", "loaded");
51 function writeToServer(name, result) {
52 var xhr = getXHRObject();
56 // asynchronously POST the results
57 xhr.open("POST", reportURL + name, true);
58 xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
62 appendStatus("XHR send failed. Error: " + e.description);
66 function postResult(name, result) {
67 writeToServer(name, result);
70 // Finish running a test by setting the status
72 function onFinished(name, id, result) {
73 // set a cookie to report the results...
74 var cookie = name + "." + id + ".status=" + result + "; path=/";
75 document.cookie = cookie;
77 // ...and POST the status back to the server
78 postResult(name, result);
81 function appendStatus(message) {
82 var statusPanel = byId("statusPanel");
84 statusPanel.innerHTML += '<BR>' + message;
88 function readCookie(name) {
89 var cookie_name = name + "=";
90 var ca = document.cookie.split(';');
92 for(var i = 0 ; i < ca.length ; i++) {
94 while (c.charAt(0) == ' ') {
95 c = c.substring(1,c.length);
97 if (c.indexOf(cookie_name) == 0) {
98 return c.substring(cookie_name.length, c.length);
104 function createCookie(name,value,days) {
107 var date = new Date();
108 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
109 expires = "; expires=" + date.toGMTString();
111 document.cookie = name+"="+value+expires+"; path=/";
114 function eraseCookie(name) {
115 createCookie(name, "", -1);
118 function isRunningInMSIE() {
119 if (/MSIE (\d+\.\d+);/.test(navigator.userAgent))
125 function reloadUsingCFProtocol() {
126 var redirect_location = "gcf:";
127 redirect_location += window.location;
128 window.location = redirect_location;
131 function isRunningInChrome() {
132 var is_chrome_frame = /chromeframe/.test(navigator.userAgent.toLowerCase());
135 var is_chrome = /chrome/.test(navigator.userAgent.toLowerCase());
139 function TestIfRunningInChrome() {
140 var is_chrome = isRunningInChrome();
142 onFailure("ChromeFrameWindowOpen", "Window Open failed :-(",
143 "User agent = " + navigator.userAgent.toLowerCase());
148 // Returns the base document url.
149 function GetBaseUrlPath() {
150 var url = window.location.protocol + "//" + window.location.host + "/";
154 // Appends arguments passed in to the base document url and returns the same.
155 function AppendArgumentsToBaseUrl() {
156 var url = GetBaseUrlPath();
157 for (arg_index = 0; arg_index < arguments.length; arg_index++) {
158 url += arguments[arg_index];
163 // Get the value of the first parameter from the query string which matches the
165 function getURLParameter(name) {
166 name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
167 var regexString = "[\\?&]" + name + "=([^&#]*)";
168 var regex = new RegExp(regexString);
169 var results = regex.exec(window.location.href);
170 if (results == null) {
177 // Create new URL by given html page name and querystring, based on current URL
179 function buildURL(pageName, queryString) {
180 var path = window.location.pathname;
183 url += window.location.protocol + "//" + window.location.host;
184 if (path.lastIndexOf("/") > 0) {
185 url += path.substring(0, path.lastIndexOf("/")) + "/" + pageName;
187 url += "/" + pageName;
189 url += ((queryString == "") ? "" : "?" + queryString);
193 // Helper function for insertControl.
194 function generateControlHtml(configuration) {
195 var objectAttributes = new Object();
196 var params = new Array();
197 var embedAttributes = new Object();
201 function stringifyAttributes(attributeCollection) {
202 var result = new String();
203 for (var attributeName in attributeCollection) {
204 result += ' ' + attributeName + '="' +
205 attributeCollection[attributeName] + '"';
210 function applyAttribute(attributeCollection, name, value, defaultValue) {
211 if (value === undefined)
212 value = defaultValue;
214 attributeCollection[name] = value;
217 objectAttributes.classid="CLSID:E0A900DF-9611-4446-86BD-4B1D47E7DB2A";
218 objectAttributes.codebase="http://www.google.com";
219 applyAttribute(objectAttributes, "id", configuration.id, "ChromeFrame");
220 applyAttribute(objectAttributes, "width", configuration.width, "500");
221 applyAttribute(objectAttributes, "height", configuration.height, "500");
223 // Attributes provided by the caller override any defaults.
224 for (var attribute in configuration.objectAttributes) {
225 if (configuration.objectAttributes[attribute] === null)
226 delete objectAttributes[attribute];
228 objectAttributes[attribute] = configuration.objectAttributes[attribute];
231 embedAttributes.type = "application/chromeframe";
233 // By default, embed id = object.id + "Plugin". null id means omit id.
234 if (embedAttributes.id === null)
235 delete embedAttributes.id;
236 else if (embedAttributes.id === undefined && objectAttributes.id !== null)
237 embedAttributes.id = objectAttributes.id + "Plugin";
239 // By default, embed name = object.id. null name means omit name.
240 if (embedAttributes.name === null)
241 delete embedAttributes.name;
242 else if (embedAttributes.name === undefined && objectAttributes.id !== null)
243 embedAttributes.name = objectAttributes.id;
245 applyAttribute(embedAttributes, "width", configuration.width, "500");
246 applyAttribute(embedAttributes, "height", configuration.height, "500");
247 applyAttribute(embedAttributes, "src", configuration.src, null);
249 for (var attribute in configuration.embedAttributes) {
250 if (configuration.embedAttributes[attribute] === null)
251 delete embedAttributes[attribute];
253 embedAttributes[attribute] = configuration.embedAttributes[attribute];
256 if (embedAttributes.src !== undefined) {
257 param = new Object();
259 param.value = embedAttributes.src;
263 // All event handlers are params and attributes of the embed object.
264 for (var eventName in configuration.eventHandlers) {
265 param = new Object();
266 param.name = eventName;
267 param.value = configuration.eventHandlers[eventName];
269 embedAttributes[eventName] = configuration.eventHandlers[eventName];
272 html = "<object" + stringifyAttributes(objectAttributes) + ">\r\n";
273 for (var i = 0; i < params.length; ++i) {
274 html += " <param" + stringifyAttributes(params[i]) + ">\r\n";
276 html += " <embed" + stringifyAttributes(embedAttributes) + "></embed>\r\n"
283 // Write the text for the Chrome Frame ActiveX control into an element.
284 // This works around a "feature" of IE versions released between April, 2006
285 // and April, 2008 that required the user to click on an ActiveX control to
286 // "activate" it. See http://msdn.microsoft.com/en-us/library/ms537508.aspx.
288 // |elementId| identifies the element in the current document into which the
289 // control markup will be inserted. |configuration| is an Object used to
290 // configure the control as below. Values shown are defaults, which may be
291 // overridden by supplying null values.
293 // "id": "ChromeFrame", // id of object tag, name of the embed tag, and
294 // // basis of id of the embed tag.
295 // "width": "500", // width of both object and embed tags.
296 // "height": "500", // height of both object and embed tags.
297 // "src": "url", // src of embed tag and of param to object tag.
298 // "eventHandlers": { // Applied to embed tag and params to object tag.
301 // "onloaderror": "..."
303 // "objectAttributes": { // Custom attributes for the object tag. Any
304 // "tabindex": "...", // properties explicitly set to null will override
305 // "border": "...", // defaults.
308 // "embedAttributes": { // Custom attributes for the embed tag;
309 // "privileged_mode": "...", // similar to above.
313 function insertControl(elementId, configuration) {
314 var e = document.getElementById(elementId);
315 e.innerHTML = generateControlHtml(configuration);