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

  • you can disable or remove input button on click of which you are calling this function – Bhushan Kawadkar Sep 28 '15 at 13:48
  • 1
    possible duplicate of [How can I dynamically unload a javascript file?](http://stackoverflow.com/questions/591685/how-can-i-dynamically-unload-a-javascript-file) – James Thorpe Sep 28 '15 at 13:50
  • @JamesThorpe each partial may have it own script(s) by itself witch doesn't referenced to a js file – Hossein Salmanian Sep 28 '15 at 13:56
  • 1
    @HosseinSalmanian It's pretty irrelevant whether a ` – James Thorpe Sep 28 '15 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. – Dave Newton Sep 28 '15 at 14:02

4 Answers4

0

var saveItems = function () {}

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

nhaa123
  • 9,570
  • 11
  • 42
  • 63
0

Try setting saveItems to undefined

   $("#partialContainer").html(returnedMarkup);
    if (!!saveItems && $.isFunction(saveItems)) saveItems = void 0;
guest271314
  • 1
  • 15
  • 104
  • 177
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.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Sergeon
  • 6,638
  • 2
  • 23
  • 43
0

You could put your function in an object literal:

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

and delete it after the ajax

delete obj.saveItems 
Sagi
  • 8,972
  • 3
  • 33
  • 41