最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript passing into a function a string containing whitespace is truncated at first whitespace? - Stack Overflow

programmeradmin4浏览0评论

can someone explain to me why you can't pass strings containing whitespace as parameters into javascript functions?

here's my code which shows this issue, just hover over the link and you'll see that only "mario" is shown as the title and not "mario and luigi"

 var href='mario and luigi', subject=$('div').data('subject'), 
 size=$('div').data('reviewid'), 
      src=$('div').data('itemid'), className='mini';

      function formatLink(href, subject, src, size, className){            
                if(size=='mini')
                size='height:25px; width:25px;';
                else if(size=='medium')
                size='height:40px; width:40px;';
                else if(size=='large')
                size='height:125px; width:125px;';
                else if(size=='xlarge')
                size='height:180px; width:260px;';                
            return '<a class="pjax" href='+href+' title='+subject+'><span class='+className+'><span class="image-wrap" style="position:relative; display:inline-block; background:url('+src+') no-repeat center center;'+size+'" ><img style="opacity:0;"></span></span><span title='+subject+'>'+subject+'</span></a>';
        }
var link=formatLink(href, subject, src, size, className);
$('div').html(link);
​

can someone explain to me why you can't pass strings containing whitespace as parameters into javascript functions?

here's my code which shows this issue, just hover over the link and you'll see that only "mario" is shown as the title and not "mario and luigi"

 var href='mario and luigi', subject=$('div').data('subject'), 
 size=$('div').data('reviewid'), 
      src=$('div').data('itemid'), className='mini';

      function formatLink(href, subject, src, size, className){            
                if(size=='mini')
                size='height:25px; width:25px;';
                else if(size=='medium')
                size='height:40px; width:40px;';
                else if(size=='large')
                size='height:125px; width:125px;';
                else if(size=='xlarge')
                size='height:180px; width:260px;';                
            return '<a class="pjax" href='+href+' title='+subject+'><span class='+className+'><span class="image-wrap" style="position:relative; display:inline-block; background:url('+src+') no-repeat center center;'+size+'" ><img style="opacity:0;"></span></span><span title='+subject+'>'+subject+'</span></a>';
        }
var link=formatLink(href, subject, src, size, className);
$('div').html(link);
​
Share Improve this question edited Apr 4, 2012 at 1:15 georg 215k56 gold badges322 silver badges400 bronze badges asked Apr 4, 2012 at 0:56 tim petersontim peterson 24.4k63 gold badges186 silver badges303 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

title='+subject+' should be title="'+subject+'". You need to quote the attribute, otherwise the markup is invalid.

As a bonus, here's an analog of python's format function which makes things like this much easier:

format = function(str, params) {
    return str.replace(/\{(\w+)\}/g, function($0, $1) { return params[$1] });
}

Usage:

html = format(
    '<a class="pjax" href="{href}" title="{subject}"><span class="{className}"... etc',
    {
        href: "some_link",
        subject: "mario and luigi",
        className: "foobar"
    }
)
return '<a class="pjax" href='+href+' title='+subject+'><span 

You don't have quotes around href attribute so your HTML ends up being href=mario and luigi. Add quotes to fix it (and do the same thing for the title):

return '<a class="pjax" href="'+href+'" title="'+subject+'"><span 

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论