Home | History | Annotate | Download | only in gaia_auth
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 function $(id) {
      6   return document.getElementById(id);
      7 }
      8 
      9 function getUrlSearchParams(search) {
     10   var params = {};
     11 
     12   if (search) {
     13     // Strips leading '?'
     14     search = search.substring(1);
     15     var pairs = search.split('&');
     16     for (var i = 0; i < pairs.length; ++i) {
     17       var pair = pairs[i].split('=');
     18       if (pair.length == 2) {
     19         params[pair[0]] = decodeURIComponent(pair[1]);
     20       } else {
     21         params[pair] = true;
     22       }
     23     }
     24   }
     25 
     26   return params;
     27 }
     28 
     29 /**
     30  * Creates a new URL which is the old URL with a GET param of key=value.
     31  * Copied from ui/webui/resources/js/util.js.
     32  * @param {string} url The base URL. There is not sanity checking on the URL so
     33  *     it must be passed in a proper format.
     34  * @param {string} key The key of the param.
     35  * @param {string} value The value of the param.
     36  * @return {string} The new URL.
     37  */
     38 function appendParam(url, key, value) {
     39   var param = encodeURIComponent(key) + '=' + encodeURIComponent(value);
     40 
     41   if (url.indexOf('?') == -1)
     42     return url + '?' + param;
     43   return url + '&' + param;
     44 }
     45 
     46 /**
     47  * Creates a new URL by striping all query parameters.
     48  * @param {string} url The original URL.
     49  * @return {string} The new URL with all query parameters stripped.
     50  */
     51 function stripParams(url) {
     52   return url.substring(0, url.indexOf('?')) || url;
     53 }
     54