2014-12-04 49 views
0

我正在爲一個創建社交鏈接鎖定器的靜態網站插件工作。如何將數據從javascript傳遞給json

我想從JavaScript傳遞數據到我的JSON,但我有一些問題。下面的代碼片段。

這有點長,但我想通過curr_page變量到JSON字符串,以便每個頁面被正確共享。如果我手動輸入一個靜態url按鈕的工作,如果我嘗試傳遞頁面或curr_page變量,它不起作用。

如何將curr_page變量傳遞給該json字符串?

<script type="text/javascript"> 
    $(document).ready(function() { 
     var curr_page = window.location.href; 
     page = String(curr_page); 
     console.log(curr_page); 
     console.log(page); 
     $("#sharer").sociallocker({ 
      text: { 
       header: "Grab the extra resources", 
       message: "Please support us, click like button below to unlock the content." 
      }, 

      locker: { 
       close: false, 
       timer: 0 
      }, 

      buttons: { 
       order: ["facebook-like", "twitter-tweet", "twitter-follow", "google-plus"] 
      }, 

      facebook: { 
       like: { 
        title: "Like", 
        url: "https://www.facebook.com/[]" 
       } 
      }, 

      twitter: { 
       tweet: { 
        title: "Tweet", 
        text: "[]", 
        url: page 
       }, 
       follow: { 
        title: "Follow us", 
        url: "https://twitter.com/[username]" 
       } 
      }, 

      google: { 
       plus: { 
        title: "google-share", 
        url: page 
       } 
      } 

     }); 
    }); 
</script> 
+0

您能提供輸出到console.log()函數嗎? – 2014-12-04 07:08:38

+0

控制檯會記錄正確的頁面,例如:http://domain.com/correct-url,但試圖使其成爲一個字符串,並將其作爲「頁面」傳遞給JSON,它不起作用,也沒有嘗試直接訪問json中的'curr_page'似乎也可以工作。 – steffan 2014-12-04 07:15:22

回答

1

那麼我希望我理解你的權利......

如果你想設置/值添加到您剛纔做這樣

myJSON.myKey = "myValue"; 
myJSON.myKey.mySubKey = "myValue"; 

這就是所有一個JSON ...

-1

通過傳遞當前頁面的URL,通過在頁面變量中提供鏈接到自身,將頁面重定向到自身。

url: page 

這裏,url指的是當前頁面而已。點擊這些社交按鈕只會重新加載相同的頁面。

0

我不確定你想要什麼,但如果你只想通過當前頁面,你可以直接放url: window.location.href

這裏要說明的是,這是一個JavaScript對象,而不是JSON字符串。您可以將變量或函數返回給url:

我不明白爲什麼不適合你。

查看我的提琴​​