字符串输出中的Javascript变量?

时间:2012-05-13 10:24:35

标签: javascript facebook-graph-api variables

变量x获取当前网址。我需要把这个url放到xxxx所在的位置。我该怎么做呢?

<script type='text/javascript'>
      function postListen()
       {
    var x = ( document.URL ); 
          FB.api(
            &#39;/me/beatmushroom:listen&#39;,
            &#39;post&#39;,
            { song:  xxxx},
            function(response) {
               if (!response || response.error) {
                  alert(&#39;Error occured&#39;);
       } else {
          alert(&#39;Listen was successful! Action ID: &#39; + response.id);
       }
    });

}   

2 个答案:

答案 0 :(得分:2)

只需在对象文字中使用该变量即可。虽然键总是一个字符串(即使没有引号),但值可以是任何JavaScript epxression:

{ recipe: x },

答案 1 :(得分:1)

只需将'xxxx'替换为变量x

即可
function postCook() {
    var x = ( document.URL );
    FB.api(
        '/me/[YOUR_APP_NAMESPACE]:cook',
        'post',
        { recipe: x },
        function(response) {
           if (!response || response.error) {
              alert('Error occured');
           } else {
              alert('Cook was successful! Action ID: ' + response.id);
           }
        }
    );
}