get URL Parameters from current URL using Prototype JavaScript

前端 未结 3 1217
轻奢々
轻奢々 2020-12-17 07:14

I\'m noob to JavaScript and want to use Prototype JS Framework to get some URL parameters. Imagine I have the following URL on my current browser:

http://www         


        
相关标签:
3条回答
  • 2020-12-17 07:36

    Expanding on Scott's answer: to put the value of the URL variable 'param' into the javascript variable 'x' you would use Prototype like so:

    x = document.URL.toQueryParams().param;
    
    0 讨论(0)
  • 2020-12-17 07:41

    Prototype.js DOES provide a utility:

    uri.toQueryParams();
    
    0 讨论(0)
  • 2020-12-17 07:47

    You really don't need Prototype for this:

    function get_param(param) {
       var search = window.location.search.substring(1);
       var compareKeyValuePair = function(pair) {
          var key_value = pair.split('=');
          var decodedKey = decodeURIComponent(key_value[0]);
          var decodedValue = decodeURIComponent(key_value[1]);
          if(decodedKey == param) return decodedValue;
          return null;
       };
    
       var comparisonResult = null;
    
       if(search.indexOf('&') > -1) {
          var params = search.split('&');
          for(var i = 0; i < params.length; i++) {
             comparisonResult = compareKeyValuePair(params[i]); 
             if(comparisonResult !== null) {
                break;
             }
          }
       } else {
          comparisonResult = compareKeyValuePair(search);
       }
    
       return comparisonResult;
    }
    
    var param_value = get_param('param'); //abc
    
    0 讨论(0)
提交回复
热议问题