简体   繁体   English

重定向后,我的JavaScript代码将无法运行

[英]After redirect, my JavaScript code won't run

I am working on a simple app using Electron and Python. 我正在使用Electron和Python开发一个简单的应用程序。 Everything works fine if I do not redirect to another page, but as soon as I do so, my entire JavaScript code wont run and application freezes. 如果我不重定向到另一个页面,则一切正常,但一旦这样做,我的整个JavaScript代码将无法运行,并且应用程序冻结。

function calculate() {
// If the below line is commented, my code below works, otherwise not. 

    window.location = "resultpage.html";

    var ps = require("python-shell")
    var path = require("path")

    var num1= document.getElementById("num1").value
    var num2 = document.getElementById("num2").value

// Shows calculated result as an alert. 
// FURTHER CODE BELOW //

I want my result to be in another HTML page (resultpage.html) but it redirects and no alert is generated. 我希望我的结果在另一个HTML页面(resultpage.html)中,但它会重定向并且不会生成警报。

Once you redirect page A (the starting page) to page B (the new page) the javascript on page A will stop running and page B will take over. 将页面A(起始页面)重定向到页面B(新页面)后,页面A上的JavaScript将停止运行,页面B将接管。 If you want to show a calculated value as an alert, the alert would need to be before the page redirect. 如果要显示计算值作为警报,则该警报将需要在页面重定向之前。 Reorder your code as follows: 重新排序您的代码,如下所示:

function calculate() { // If the below line is commented, my code below works, otherwise not. 函数calculate(){//如果注释以下行,则下面的代码有效,否则无效。

var ps = require("python-shell")
var path = require("path")

var num1= document.getElementById("num1").value
var num2 = document.getElementById("num2").value

// Shows calculated result as an alert here. 

//redirect after alert
window.location = "resultpage.html";


// FURTHER CODE BELOW //

The other thing to note, is that the elements num1 and num2 on page A will no longer be on page B. Should you want to run the calculation on page B, you will need to send that data as part of the URL as a parameter if you want it accessible and then either populate the fields on the page with the correct values, or change your javascript to read the values from the parameters. 还要注意的另一件事是,页面A上的元素num1和num2将不再位于页面B上。如果要在页面B上运行计算,则需要将该数据作为URL的一部分发送作为参数。如果您希望它可以访问,则可以使用正确的值填充页面上的字段,或者更改JavaScript以从参数中读取值。

When you redirect the user, it will not carry the previous page JS code to the target page. 当您重定向用户时,它将不会将上一页JS代码携带到目标页面。 Therefore you would need to calculate the result either on the target page or pass the result to the target page as a URL query or some other way. 因此,您将需要在目标页面上计算结果,或者将结果作为URL查询或其他方式传递给目标页面。

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

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