Ask coding questions

← Back to all posts
Prompt opening up a URL when cancled in javascript
MikeJMS8910 (234)

So in my project( when you press the open save button a prompt comes up that asks for the name of the save file. Opening the save works but for some reason when you press cancel it opens up a random URL so can anyone help me. Also if you go on the second page and press cancel on the create save prompt it just says that the name is already taken which I made it for if the key for the name was already taken.

19wintersp (1130)

This could happen if you call localStorage.setItem with null as the key. In the save function, if the user presses "Cancel", fileSaveName gets set to null. Then, when the user presses "Cancel" on the load prompt, it calls localStorage.getItem(null), which returns what was saved. Summary:

  1. Save function is called
  2. Filename prompt is displayed
  3. User presses "Cancel"
  4. fileSaveName is set to null
  5. localStorage.setItem(null, pageUrl)
  6. Later, open function is called
  7. Filename prompt is displayed
  8. User presses "Cancel"
  9. loadSave is set to null
  10. localStorage.getItem(null
  11. Whatever was set at step 5 is opened
SixBeeps (5629)

I was unable to replicate the random URL issue, pressing cancel just makes it say "That name is invalid!" and nothing else.

As for the name already taken part, I'm not sure where the "second page" is located. Do you perchance have a link to it?

MikeJMS8910 (234)

@SixBeeps the second page comes up after you press calculate