在带有a-tag的onclick()-event之后重定向

时间:2018-09-09 09:17:50

标签: javascript html electron

我正在用elecrtonjs编写一个应用程序,并被卡住了。

所以在我的页面上有以下HTML:

<a href="./step1.html" id="next"><span onclick="writeToFile();return false">Next ></span></a`>

然后在javasriptfile中,我将东西保存到目录中的文件中:

var fs = require('fs');

function writeToFile() {
    //alert(document.getElementsByTagName("input").length);
    var text = "";
    // Change the content of the file as you want
    // or either set fileContent to null to create an empty file
    for (var i = document.getElementsByTagName("input").length - 1; i >= 0; i--) {
        text += document.getElementsByTagName("input")[i].getAttribute("name")+":\n";
        text += document.getElementsByTagName("input")[i].value+"\n";
    }

    // The absolute path of the new file with its name
    var filepath = "mynewfile.txt";
    fs.appendFile(filepath, text, (err) => {
        if (err) throw err;

        console.log("The file was succesfully saved!");
    });
 }

代码可以正确重定向,但不会将用户输入附加到指定文件中。

我将时间安排错了吗?我尝试过

onbeforeunload

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

Chris Li 所述,您应该使用

GraphServiceClient

并删除链接的href字段。