使用 Prototype JavaScript 从当前 URL 获取 URL 参数 [英] get URL Parameters from current URL using Prototype JavaScript

查看:20
本文介绍了使用 Prototype JavaScript 从当前 URL 获取 URL 参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 JavaScript 的菜鸟,想使用 Prototype JS 框架来获取一些 URL 参数.假设我在当前浏览器上有以下 URL:

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.somewhere.com?param=abc

如何使用 Prototype JS<的任何函数或实用程序获取 'param' 的值/a> ?

how can I get the value of 'param' using any function or utility of Prototype JS ?

推荐答案

你真的不需要 Prototype:

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

这篇关于使用 Prototype JavaScript 从当前 URL 获取 URL 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆