2 Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 Use of this source code is governed by a BSD-style license that can be
4 found in the LICENSE file.
8 * @fileoverview Common methods for performance-plotting Javascript.
12 * Fetches a URL asynchronously and invokes a callback when complete.
14 * @param {string} url URL to fetch.
15 * @param {Function(string, string)} callback The function to invoke when the
16 * results of the URL fetch are complete. The function should accept two
17 * strings representing the URL data, and any errors, respectively.
19 function Fetch(url, callback) {
20 var r = new XMLHttpRequest();
21 r.open('GET', url, true);
22 r.setRequestHeader('pragma', 'no-cache');
23 r.setRequestHeader('cache-control', 'no-cache');
25 r.onreadystatechange = function() {
26 if (r.readyState == 4) {
27 var text = r.responseText;
30 error = url + ': ' + r.status + ': ' + r.statusText;
32 error = url + ': null response';
33 callback(text, error);
41 * Parses the parameters of the current page's URL.
43 * @return {Object} An object with properties given by the parameters specified
44 * in the URL's query string.
46 function ParseParams() {
47 var result = new Object();
49 var query = window.location.search.substring(1)
50 if (query.charAt(query.length - 1) == '/')
51 query = query.substring(0, query.length - 1) // Strip trailing slash.
52 var s = query.split('&');
54 for (i = 0; i < s.length; ++i) {
55 var v = s[i].split('=');
57 var value = unescape(v[1]);
61 if ('history' in result) {
62 result['history'] = parseInt(result['history']);
63 result['history'] = Math.max(result['history'], 2);
65 if ('rev' in result) {
66 result['rev'] = parseInt(result['rev']);
67 result['rev'] = Math.max(result['rev'], -1);
74 * Creates the URL constructed from the current pathname and the given params.
76 * @param {Object} An object containing parameters for a URL query string.
77 * @return {string} The URL constructed from the given params.
79 function MakeURL(params) {
80 var url = window.location.pathname;
85 url += sep + p + '=' + params[p];