使用 javascript 创建包含多个条目的单个 VCard 文件



我使用了这个githubgist中的代码,它允许您将单个内容保存到VCF文件中。

<!doctype html>
<html>
    <head>
       <script type="text/javascript" src="vcard2.js"></script>
    </head>
    <script type="text/javascript">
        // From JSON
        var johnSmith = {
            "version": "4.0",
            "n": "SMITH;John;;",
            "fn": "John Smith",
            "nickname":"js",
            "title": "Missing man too",
            "tel": [
                {"value": "555-555-555", "type": "cell"}
            ],
            "email": [
                { "value": "john.smith@work.com", "type": "work" },
                { "value": "john.smith@home.com", "type": "home" }
            ]
        }
        var link = vCard.export(johnSmith, "John Smith", false) // use parameter true to force download
        document.body.appendChild(link)
        </script>
    </body>
</html>

这适用于单个内容作为文件,请参阅 JSfiddle。我的问题是如何将多个内容添加到同一个VCF文件中。

在您提供的小提琴中,只需编辑 dump() 函数即可接受数组。然后迭代数组并执行完全相同的操作,将输出(与n拆分(组合成一个变量。

    dump: function(cards) {
        var cardsLength = cards.length;
        var cardsStr = "";
        for (var cardsIndex = 0; cardsIndex < cardsLength; cardsIndex++) {
            var card = cards[cardsIndex];
            //...existing code... just remove the return
             cardsStr+=str+"n";
        }
        return cardsStr;
    }

现在调用 vCard.export() 并传递对象数组而不是对象。

在这里试试: JSFiddle

最新更新