简体   繁体   English

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

[英]Redirecting after onclick()-event with a-tag

I am programming an app with elecrtonjs and got stuck. 我正在使用elecrtonjs编写应用程序,并被卡住。

So on my page there is this HTML: 因此,在我的页面上有以下HTML:

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

And in the javasriptfile I am saving stuff to a file in the directory: 在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!");
    });
 }

The code redirects properly but does not append the user-input in to the specified file. 该代码可以正确重定向,但不会将用户输入附加到指定文件中。

Am I timing the stuff wrong? 我将时间安排错了吗? I tried 我试过了

onbeforeunload

Thanks for the help. 谢谢您的帮助。

As Chris Li already stated, you should use 正如克里斯·李(Chris Li)所述,您应该使用

window.location.href = "./step1.html"

and remove the href field of your link. 并删除链接的href字段。

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

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