2012-08-29 147 views
0

你好,我試圖開發的Facebook的Open Graph系統爲我的網站,我幾乎已經完成......如何在Javascript中顯示Javascript變量?

它需要當前頁面的URL被張貼到運行action..I使用文檔了當前網頁的網址.URL到稱爲BASE Java腳本變量,但我不知道如何把它放在一個javascript裏面,因爲它總是顯示URL的變量名這一翻譯,當我看着頁面源代碼...

var base = document.URL; 

    function postCook() 
    { 

     FB.api(
     '/me/video.watches', 
     'post', 
     { video: CURRENT PAGE URL SHOULD BE HERE}, 
     function(response) { 
      if (!response || response.error) { 
       alert('Error occured'); 
      } else { 
       alert('Cook was successful! Action ID: ' + response.id); 
      } 
     }); 


    } 

回答

1

當你看在頁面源代碼中,您可以看到從服務器發送的代碼。只要把變量放在那裏,它應該工作。

var myURL = document.URL; 
FB.api(
    '/me/video.watches', 
    'post', 
    { video: myURL }, 
    function(response) { 
     if (!response || response.error) { 
      alert('Error occured'); 
     } else { 
      alert('Cook was successful! Action ID: ' + response.id); 
     } 
    }); 

FB.api(
    '/me/video.watches', 
    'post', 
    { video: window.location.href }, 
    function(response) { 
     if (!response || response.error) { 
      alert('Error occured'); 
     } else { 
      alert('Cook was successful! Action ID: ' + response.id); 
     } 
    }); 
+0

感謝....!它的工作原理...不得不說,我試過視頻:document.url和視頻:基地之前,但出來只是這些名字,讓我擔心......!我明白了..再次感謝.. –

1

我用這個在我的應用程序:

FB.api('/me/namespace:action&object='+encodeURIComponent(location.href),'post', function(response) { 
     if (!response || response.error) { 
      alert('Error occured'); 
     } else { 
      alert('Cook was successful! Action ID: ' + response.id); 
     } 
    });