[英]how to pass data from javascript to json
我正在為一個靜態網站創建一個社交鏈接儲物櫃插件。
我正在嘗試將數據從javascript傳遞到json,但出現了一些問題。 下面的代碼段。
它有點長,但我正在嘗試將curr_page變量傳遞到json字符串中,以便正確共享每個頁面。 如果我手動輸入靜態網址,則按鈕會起作用,如果我嘗試傳遞page或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>
好吧,希望我能正確理解你...
如果您想為JSON設置/添加值,就可以像
myJSON.myKey = "myValue";
myJSON.myKey.mySubKey = "myValue";
就這樣...
我不確定您想要什么,但是如果您只想傳遞當前頁面,則可以直接放置url: window.location.href
。
在這里要清楚,這是一個javascript對象,而不是JSON字符串。 您可以傳遞變量或從函數返回url:
我不明白為什么對您不起作用。
在這里檢查我的小提琴
通過傳遞當前頁面的URL,您可以通過在頁面變量中提供指向自身的鏈接來將頁面重定向到自身。
url: page
在這里,該URL僅指當前頁面。 並且點擊這些社交按鈕只會重新加載同一頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.