简体   繁体   中英

javascript function not defined when hit back button

I have the following example files:

index.html

<body>
  <input type="file" name="image" onchange="handleUpload()">
  <script>
    function handleUpload() {
      window.location = 'aa.html';
    }
  </script>
</body>

aa.html

<body>
  success
</body>

After file upload I'm redirected to another page. When I hit browser back button I get Uncaught ReferenceError: handleUpload is not defined at HTMLInputElement.onchange .

Can someone explain why is this happening? I understand why handleUpload is triggered but why it's undefined?

Update

My app is more complex. I use webpack to bundle my js files into a single file then I reference it in layout before closing body tag. What i'm trying to do is a image preview when input file is filled. Everything works great but if I hit back button after the form was submitted I get that error.

I manage to avoid this problem by removing onchange from html and added a event listener in one of js file. This approach has a delay on opening the select file window.

这是你的答案

  <input type="file" name="image" onchange="window.location='aa.html'"> 

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