简体   繁体   English

为什么在我的javascript变量中的空格中添加%20

[英]Why is it adding %20 to the spaces in my javascript variables

function openFile(file, object) {
    var extension = file.substr( (file.lastIndexOf('.') +1) );

    var fileName = file.substr((file.lastIndexOf('/') +1), (file.length - (file.lastIndexOf('/') +1))-4);


    object.append('<img class="theimage" src="" alt="icon"/>');
    object.append('<span class="thefile"></span>');


    switch(extension) {

        case 'ppt':
            object.find('img').attr('src', 'PowerPoint-icon.png');
        break;
        case 'pdf':
            object.find('img').attr('src', 'pdficon_large.gif'); 
        break;
        case 'txt':
            object.find('img').attr('src', 'txt_icon.png'); 
        break;
        default:
            alert('error');
    }

    object.find('span.thefile').text(fileName);

};

This function runs properly on it's own but when I add it to my school's cms template it add %20 to all the spaces in fileName. 此函数本身可以正常运行,但是当我将其添加到学校的cms模板时,它将%20添加到fileName中的所有空格。

Do you think they have their own function that is doing this? 您认为他们有自己的职能吗? What would be the purpose? 目的是什么? For security? 为了安全?

%20 is standard URL encoding for spaces. %20是空格的标准URL编码。 Whatever function is processing the spaces thinks they need to be encoded for use in a URL. 无论使用什么功能处理空格,都认为需要对空格进行编码才能在URL中使用。

As to why it is done, it is not exactly for security. 至于为什么这样做,并不是完全出于安全考虑。 Not all systems handle spaces well; 并非所有系统都能很好地处理空间。 this helps in those cases, so it is considered 'safer' to URL encode spaces (among other things). 在这种情况下这很有帮助,因此,URL编码空格(除其他事项外)被认为是“更安全”的。 For all other information see RFC 3986 s2.1 and s2.4 有关所有其他信息,请参阅RFC 3986 s2.1s2.4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM