首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将数据从javascript传递给json

如何将数据从javascript传递给json
EN

Stack Overflow用户
提问于 2014-12-04 07:05:36
回答 3查看 91关注 0票数 0

我正在为一个静态网站开发一个插件,它创建了一种社交链接储物柜。

我试图将数据从javascript传递给我的json,但是我遇到了一些问题。下面的代码片段。

它有点长,但我正在尝试将curr_page变量传递到json字符串中,以便正确地共享每个页面。如果我手动输入一个静态url按钮工作,如果我试图传递页面或curr_page变量,它不工作。

如何将curr_page变量传递到json字符串中?

代码语言:javascript
复制
<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>
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-12-04 07:18:54

我希望我能理解你的意思。

如果要为JSON设置/添加值,只需执行以下操作

代码语言:javascript
复制
myJSON.myKey = "myValue";
myJSON.myKey.mySubKey = "myValue";

就这些..。

票数 1
EN

Stack Overflow用户

发布于 2014-12-04 07:47:05

我不知道你想要什么,但是如果你只想传递当前页面,你可以直接放url: window.location.href

这里要说明的是,这是一个javascript对象,而不是JSON字符串。您可以向url:传递变量或从函数返回。

我不明白为什么不为你工作。

检查我的Fiddle 这里

票数 0
EN

Stack Overflow用户

发布于 2014-12-04 07:11:21

通过传递当前页面url,您将页面重定向到它自己,并在页变量中提供到它自己的链接。

代码语言:javascript
复制
   url: page

在这里,url仅指当前页面。而这些点击的社交按钮只会重新加载相同的页面。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27288138

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档