我有这段代码将 URL 参数拉入登陆页面,问题是它将空格拉入为 %20。所以如果我的 url 参数是: example.com/?title=my website 它将在页面上显示 my%20website 我希望它显示我的网站而不带 %20。这是代码
function GetURLParameter(sParam)
{
var sPageURL = window.location.search.substring(1);
var sURLVariables = sPageURL.split('&');
for (var i = 0; i < sURLVariables.length; i++)
{
var sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] == sParam)
{
return sParameterName[1];
}
}
}
(function ($) {
// fire once DOM is loaded
$(document).ready(function() {
// set the cookie name
var cookie_name = 'node-title';
// get the "phone" URL param
var phone_number = GetURLParameter('title');
// check if there is a phone number in the URL
if (phone_number) {
// set the cookie
$.cookie(cookie_name, phone_number, { path: '/' });
}
// get the phone cookie value
var phone_cookie = $.cookie(cookie_name);
// check if there is a value set in the phone cookie
if (phone_cookie) {
// swap the phone number
$('.' + cookie_name).html(phone_cookie);
// update the href too
$('a.' + cookie_name).attr('href', 'tel://' + phone_cookie);
}
});
})(jQuery);
最佳答案
只需在返回之前对值进行 url 解码即可。替换
return sParameterName[1];
与
return decodeURIComponent(sParameterName[1]);
关于javascript - 函数 GetURLParameter(sParam) 返回 %20 作为页面上的空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26679341/