简体   繁体   中英

After redirect, my JavaScript code won't run

I am working on a simple app using Electron and 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.

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.

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. 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.

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.

When you redirect the user, it will not carry the previous page JS code to the target page. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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