Johanna
Johanna

Reputation: 39

Stop a jQuery script

I would like to know if it possible (and how) to stop a jQuery script on a click event.

I am loading it on a click like this

$("#id_button").click(function() { $.getScript("script.js"); alert ("The script is activated"); });

And I'd like to stop it in the same way.

Upvotes: 4

Views: 819

Answers (2)

Alexis Abril
Alexis Abril

Reputation: 6469

If this is regarding the dynamic "unloading" of a js file, you could apply Jonathan Sampson's approach.

Basically "script.js" could be encapsulated in an object and upon "click", dynamically loaded. On the next click event, different button click or event of your choice, you can set that object to null, removing that script's capabilities.

Edit: Rebecca Murphy's slides on code organization.

Upvotes: 0

rahul
rahul

Reputation: 187110

I am not sure whether you are lokking for this one or not.

You can bind and unbind an event using jquery like

$("#id_button").bind("click", function(){
});

and for unbind

$("#id_button").unbind("click");

Upvotes: 1

Related Questions