javascript - 函数 GetURLParameter(sParam) 返回 %20 作为页面上的空白

标签 javascript jquery

我有这段代码将 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/

相关文章:

javascript - jquery:卸载还是卸载之前?

javascript - jquery 解析 json 时出现问题

javascript - 使用 Observable 排队 promise

javascript - 如何将触发器从 input[type ="file"] 更改为 span 标记?

javascript - 当响应式样式不再适用时删除类

javascript - 如何在 CSS 代码之前加载 jQuery?

javascript - height.js 代码内的链接

javascript - 无法读取未定义错误的属性推送

javascript - jQuery 从字符串中删除字符串

javascript - 当增加十的幂(1000、10000、100000 等)时,Jquery 绑定(bind)数字输入不起作用