简体   繁体   中英

vb.net JS function defined in head is not defined at onLoad

I have a vb.net application, and within the application, there is the functionality to submit a report.

When the user has answered all of the questions on the report, they click the 'Submit' button, and are presented with a page displaying a form requiring them to enter a password- once they have entered a password (twice- 'Password' field & 'Confirm Password' field), they click the 'Continue' button, and are taken to a page displaying a 'Report Submitted' message, and a case number for the report they have submitted. At this point, the report is submitted, and the information recorded in the database.

The user can use the case number they're given & the password they create to log back in to the system, to check for any feedback that has been added to their report by their employer.

However, there is an issue that if a user fills out a report, and clicks the 'Submit' button, if they click the browser 'Back' button, or navigate back a page in another way when they're viewing the 'Enter Password' page, they are taken back to the page on which they filled out the report, and all of their previously entered values are still displayed there.

If they then click the Submit button again (maybe they changed some of the data previously entered, maybe not), then a second report is generated & the information is recorded in the database a second time- so this can cause duplicate records.

I want to try and prevent the user being able to navigate back a page during this two-step phase of submitting the report.

I came across this answer on SO, and have tried implementing it within my project.

However, when I now click 'Submit' to submit a report once completed, my browser console displays an error stating:

Uncaught ReferenceError: changeHashOnLoad is not defined at onload (passwordconfirm:118)

I added the function & function call in passwordconfirm.aspx:

<head runat="server">
    ...
    <script>
        $(document).ready(function () {
            ...
            function changeHashOnLoad() {
                console.log("changeHashOnLoad() called ");
                window.location.href += "#";
                setTimeout("changeHashAgain()", "50");
            }

            function changeHashAgain() {
                window.location.href += "1";
            }

            var storedHash = window.location.hash;
            window.setInterval(function() {
                if (window.location.hash != storedHash) {
                    window.location.hash = storedHash;
                }
            }, 50);
            ...
        });
    </script>
</head>
<body onload="changeHashOnLoad(); ">
    ...
</body>

But I'm not sure why I'm getting the Uncaught ReferenceError stating that changeHashOnLoad is not defined when submitting a report, and the submission confirmation page is loaded...

Is it because the onload property of the HTML tag is run before the code within the <script></script> tags of the HTML <head></head> section? If so, how can I ensure that the changeHashOnLoad() function is already defined by the time the page is loaded- where should I be defining that function?

Or, if not, what am I doing wrong here?

You are declaring your functions within the document.ready callback function. This means you hash functions will be locally scoped and will only exist in that function scope. Instead do the following:

function changeHashOnLoad() {
  console.log("changeHashOnLoad() called ");
  window.location.href += "#";
  setTimeout("changeHashAgain()", "50");
}

function changeHashAgain() {
  window.location.href += "1";
}

$(document).ready(function () {
  var storedHash = window.location.hash;
  window.setInterval(function () {
    if (window.location.hash != storedHash) {
      window.location.hash = storedHash;
    }
  }, 50);
});

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