1

let's suppose the browser rendered following page

<html>
...
<body>
<div id="partialContainer">
<script>
function saveItems(){
    /* do somthing*/
}
</script>
<input type="button" id="btnTest" name="btnTest" value="Test" onclick="saveItems()"/>
</div>
</body>
</html>

after an AJAX call and change the "partialContainer" content using

$("#partialContainer").html(returnedMarkup)

saveItems function still remains in page and can get executed how can remove this function when markup get replaced to avoid name colissioning

5
  • you can disable or remove input button on click of which you are calling this function Commented Sep 28, 2015 at 13:48
  • 1
    possible duplicate of How can I dynamically unload a javascript file? Commented Sep 28, 2015 at 13:50
  • @JamesThorpe each partial may have it own script(s) by itself witch doesn't referenced to a js file Commented Sep 28, 2015 at 13:56
  • 1
    @HosseinSalmanian It's pretty irrelevant whether a <script> tag has script within itself or is including an external file - once the file is loaded and executed, it may as well have been inline, ie the answers are more or less the same whether you have inline script or external files. Commented Sep 28, 2015 at 13:57
  • How does it get executed? If you need to change page functionality at the UI level then do it at the UI level rather than mucking with underlying functionality. Commented Sep 28, 2015 at 14:02

4 Answers 4

0

var saveItems = function () {}

After your Ajax, assign some other value to it, preferably the one above.

Sign up to request clarification or add additional context in comments.

Comments

0

Try setting saveItems to undefined

   $("#partialContainer").html(returnedMarkup);
    if (!!saveItems && $.isFunction(saveItems)) saveItems = void 0;

1 Comment

i want to remove each function used in a dome element witch is replaced
0

You could put your function in an object literal:

var obj = { saveItems: function() { } }

and delete it after the ajax

delete obj.saveItems 

Comments

0

On your ajax success callback function, just do:

$("#btnTest").prop( "onclick", null );

Be aware that $.removeAttr('onclick') will fail in ie 6-8, so .prop() is better.

2 Comments

the main problem is that btnTest already removed from DOM but the function still remains and may cause function colissioning
Oh, i see. Did you try: " delete saveItems; " ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.