简体   繁体   English

如何将数据从javascript传递到json

[英]how to pass data from javascript to json

I am working on a plugin for a static site that creates a sort of social link locker. 我正在为一个静态网站创建一个社交链接储物柜插件。

I am trying to pass data from the javascript to my json but i am having some issues. 我正在尝试将数据从javascript传递到json,但出现了一些问题。 Code snippet below. 下面的代码段。

It's a bit long but i'm trying to pass the curr_page variable into the json string so that each page is shared properly. 它有点长,但我正在尝试将curr_page变量传递到json字符串中,以便正确共享每个页面。 If i manually type in a static url the buttons work, if I try to pass in the page or curr_page variable, it doesn't work. 如果我手动输入静态网址,则按钮会起作用,如果我尝试传递page或curr_page变量,则它将不起作用。

How can I pass the curr_page variable into that json string? 如何将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>

Well I hope I understand you right... 好吧,希望我能正确理解你...

If you want to set / add a value to a JSON you just do it like 如果您想为JSON设置/添加值,就可以像

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

Thats all... 就这样...

I'm not sure what do you want, but if you only want to pass the current page you can put directly url: window.location.href . 我不确定您想要什么,但是如果您只想传递当前页面,则可以直接放置url: window.location.href

To be clear here, this is a javascript object, not a JSON string. 在这里要清楚,这是一个javascript对象,而不是JSON字符串。 You can pass variables or return from a function to the url: . 您可以传递变量或从函数返回url:

I can't understand why isn't working for you. 我不明白为什么对您不起作用。

Check my Fiddle here 在这里检查我的小提琴

By passing the current page url, you are redirecting the page to itself, by providing the link to itself, in page variable. 通过传递当前页面的URL,您可以通过在页面变量中提供指向自身的链接来将页面重定向到自身。

   url: page

here, the url is referring to the current page only. 在这里,该URL仅指当前页面。 and these social buttons on clicking will only reload the same page. 并且点击这些社交按钮只会重新加载同一页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM