jQuery:来自 URL 的变量,带有 ÆØÅ还有什么不是
我使用脚本从 URL 获取变量并对其进行解码 - 准确地说是“userName”。
但是,该脚本仅适用于英语字符,不适用于丹麦语或其他拉丁字符。我怎样才能通过这个?
如果名称是“Jørgen-Vælle Jürgensen”,则脚本将输出“Jårgen-Vælle Jürgensen”...不是我所期望的安静...:-)
脚本:
//Get the variable userName from URL
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
while(x<c.length){var m=r.exec(c.substr(x));
if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
}else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});
$.extend({
getUrlVars: function () {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function (name) {
return $.getUrlVars()[name];
}
});
_allVars = $.getUrlVars();
var userName = _allVars.userName;
$(".userName").text($.URLDecode(userName));
应该做什么我愿意??
先感谢您。
I use a script to get a variable from the URL and decode it - "userName" to be preciese.
However the script only works with English characters, and not Danish or other Latin characters. How can I get pass this?
If the name is "Jørgen-Vælle Jürgensen" then the script will output "Jørgen-Vælle Jürgensen" ... not quiet what I expected... :-)
Script:
//Get the variable userName from URL
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();var r=/(^[a-zA-Z0-9_.]*)/;
while(x<c.length){var m=r.exec(c.substr(x));
if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
}else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){b=parseInt(m[1].substr(1),16);
t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});
$.extend({
getUrlVars: function () {
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for (var i = 0; i < hashes.length; i++) {
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
},
getUrlVar: function (name) {
return $.getUrlVars()[name];
}
});
_allVars = $.getUrlVars();
var userName = _allVars.userName;
$(".userName").text($.URLDecode(userName));
What shouuld I do??
Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
请参阅 decodeURIComponent
See decodeURIComponent
您需要解码 网址。
$.URLDecode(url)
是你的朋友 =) 这是一个插件(可能有 jQuery 或纯 JavaScript 中的内置方法,但这是 Google 上的第一个命中),所以你会下载并包含它以使其正常工作。You need to decode the url.
$.URLDecode(url)
is your friend =) It's a plugin (there might be a built-in way in jQuery or plain javascript, but this was the first hit on Google) so you'll have to download and include that for it to work.使用javascript的decodeURIComponent函数来解码url。
use javascript decodeURIComponent function to decode the url.
代替:
使用这个
答案是仅使用 JavaScript,这使得它非常简单。
Instead of:
Use this
The answer is to use JavaScript only, which makes it very simple.