开发者

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

开发者 https://www.devze.com 2023-01-19 13:51 出处:网络
function openFile(file, object) { var extension = file.substr( (file.lastIndexOf(\'.\') +1) ); var fileName = file.substr((file.lastIndexOf(\'/\') +1), (file.length - (file.lastIndexOf(\'/\') +1))-4)
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'开发者_StackOverflow社区);
        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.

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. Whatever function is processing the spaces thinks they need to be encoded for use in a 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). For all other information see RFC 3986 s2.1 and s2.4

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号