0

I'm dynamically changing a script tag's src attribute using JavaScript.

Problem is, that I'd like to "undo" the previous JavaScript before re-executing it. Any way to do this?

Thanks,

g3

JordyvD
  • 1,565
  • 1
  • 18
  • 45
  • Why do you add it in the first place? Ever thought about [loading it with ajax](https://api.jquery.com/jquery.getscript/) ? – joschuck Apr 03 '15 at 18:27
  • I think you will find the answer here. http://stackoverflow.com/questions/9642205/how-to-force-a-script-reload-and-re-execute – Sohel Apr 03 '15 at 18:27
  • The reason I add it is to add links to a navbar, however i want these links to change. (It's a packaged chrome app made with polymer) – JordyvD Apr 03 '15 at 18:28
  • Thanks @Sohel, I actually found that post before asking, but I was wondering if there was any easy-undo function. I'll do it a different way I guess ^^ – JordyvD Apr 03 '15 at 18:30
  • Can you define "undo"? If all you did was instantiate a bunch of stuff, you could always just delete or otherwise overwrite that in-memory "stuff". Otherwise your solution potentially could be considerably more complex. – dudewad Apr 03 '15 at 18:35
  • The first code creates a bunch of nodes and I want to get rid of them on the second run, but they share a class so I can just destroy them nonetheless ":) – JordyvD Apr 03 '15 at 18:41

1 Answers1

0

I can describe a method I used to achieve a partial undo in a project. Ugly, but might be useful. First, you may need to refactor your script to work from within an iframe, and/or wrap it in a with statement to give it easy access to some objects in the parent (don't even think of with(opener); you'll want something more precise like with({someObject: opener.someObject}).

When your script runs, it can use the iframe's global scope and DOM freely. Then, instead of reloading the <script> tag, you overwrite the whole iframe, which will reset everything your script did inside the iframe.

Touffy
  • 6,309
  • 22
  • 28