获取转义的网址参数

本文介绍了如何在JavaScript中安全地获取和解析URL参数,特别是处理包含特殊字符的参数。推荐使用jQuery URL Parser插件,它能有效地处理各种URL,提供获取参数值和解析完整URL信息的功能。
摘要由CSDN通过智能技术生成

本文翻译自:Get escaped URL parameter

I'm looking for a jQuery plugin that can get URL parameters, and support this search string without outputting the JavaScript error: "malformed URI sequence". 我正在寻找一个可以获取URL参数的jQuery插件,并支持此搜索字符串而不输出JavaScript错误:“格式错误的URI序列”。 If there isn't a jQuery plugin that supports this, I need to know how to modify it to support this. 如果没有支持这个的jQuery插件,我需要知道如何修改它来支持这个。

?search=%E6%F8%E5

The value of the URL parameter, when decoded, should be: 解码后,URL参数的值应为:

æøå

(the characters are Norwegian). (字符是挪威语)。

I don't have access to the server, so I can't modify anything on it. 我无权访问服务器,因此无法对其进行任何修改。


#1楼

参考:https://stackoom.com/question/5tDM/获取转义的网址参数


#2楼

$.urlParam = function(name){
  var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(top.window.location.href); 
  return (results !== null) ? results[1] : 0;
}

$.urlParam("key");

#3楼

What you really want is the jQuery URL Parser plugin . 你真正想要的是jQuery URL Parser插件 With this plugin, getting the value of a specific URL parameter (for the current URL) looks like this: 使用此插件,获取特定URL参数(对于当前URL)的值如下所示:

$.url().param('foo');

If you want an object with parameter names as keys and parameter values as values, you'd just call param() without an argument, like this: 如果你想要一个带有参数名称的对象作为键和参数值作为值,你只需要调用没有参数的param() ,如下所示:

$.url().param();

This library also works with other urls, not just the current one: 此库也适用于其他网址,而不仅仅是当前网址:

$.url('http://allmarkedup.com?sky=blue&grass=green').param();
$('#myElement').url().param(); // works with elements that have 'src', 'href' or 'action' attributes

Since this is an entire URL parsing library, you can also get other information from the URL, like the port specified, or the path, protocol etc: 由于这是一个完整的URL解析库,您还可以从URL获取其他信息,如指定的端口,或路径,协议等:

var url = $.url('http://allmarkedup.com/folder/dir/index.html?item=value');
url.attr('protocol'); // returns 'http'
url.attr('path'); // returns '/folder/dir/index.html'

It has other features as well, check out its homepage for more docs and examples. 它还有其他功能,请查看其主页以获取更多文档和示例。

Instead of writing your own URI parser for this specific purpose that kinda works in most cases, use an actual URI parser. 而不是写你自己的URI解析器这有点儿工作在大多数情况下,这一特定目的的,用一个实际的URI解析器。 Depending on the answer, code from other answers can return 'null' instead of null , doesn't work with empty parameters ( ?foo=&bar=x ), can't parse and return all parameters at once, repeats the work if you repeatedly query the URL for parameters etc. 根据答案,其他答案的代码可以返回'null'而不是null ,不能用空参数( ?foo=&bar=x ),不能一次解析并返回所有参数,重复工作如果你反复查询参数的URL等。

Use an actual URI parser, don't invent your own. 使用实际的URI解析器,不要发明自己的。

For those averse to jQuery, there's a version of the plugin that's pure JS . 对于那些厌恶jQuery的人来说,有一个纯JS的插件版本


#4楼

I created a simple function to get URL parameter in JavaScript from a URL like this: 我创建了一个简单的函数来从URL获取URL参数,如下所示:

.....58e/web/viewer.html?page=*17*&getinfo=33


function buildLinkb(param) {
    var val = document.URL;
    var url = val.substr(val.indexOf(param))  
    var n=parseInt(url.replace(param+"=",""));
    alert(n+1); 
}
buildLinkb("page");

OUTPUT: 18 输出: 18


#5楼

This may help. 这可能有所帮助。

<script type="text/javascript">
    $(document).ready(function(){
        alert(getParameterByName("third"));
    });
    function getParameterByName(name){
        var url     = document.URL,
            count   = url.indexOf(name);
            sub     = url.substring(count);
            amper   = sub.indexOf("&"); 

        if(amper == "-1"){
            var param = sub.split("=");
            return param[1];
        }else{
            var param = sub.substr(0,amper).split("=");
            return param[1];
        }

    }
</script>

#6楼

Just in case you guys have the url like localhost/index.xsp?a=1#something and you need to get the param not the hash. 万一你们有像localhost / index.xsp这样的网址?a = 1#的东西,你需要得到的参数不是哈希。

var vars = [], hash, anchor;
var q = document.URL.split('?')[1];
if(q != undefined){
    q = q.split('&');
    for(var i = 0; i < q.length; i++){
        hash = q[i].split('=');
        anchor = hash[1].split('#');
        vars.push(anchor[0]);
        vars[hash[0]] = anchor[0];
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值