2

I am sending an ajax request to one of my controller to update the user interaction (which page he visits/likes) for an very insight analytics. I am storing these information in my mongo db.

All I want is, on success of this request, delete this script. But all the alert works, but the script never deletes. The following is my code

<div id="delete_this">
<script>
$(document).ready(function() {
  $.ajax({
    url: weblink+'user-interactions',
    type: 'POST',
    dataType: 'html',
    data: {
        //some post data
    },
   })
   .done(function(html) {
        alert("works");
        var status = "executed";
        alert("works here");
            $("#delete_this").remove();

    })
    .fail(function(html) {
        console.log("error");
    });


});
</script>
</div>

WHAT I HAVE DONE TILL NOW:

1) tried with adding a div as the parent and pointing to delete that div as shown in script.

2) separated out the .remove() from the script into a new script tag and used something like this.

<script>
$(document).ready(function() {
    $("#delete_this").remove();
});
</script>
  1. tried to specify the parentnode and delete the child.

I failed in all three attempts. I am really stuck out here.

Any reasons why it is not working?

I also have another question relating to this.

This link says that the javascript stays in the session till the page is refreshed. So why are'nt we following a standard where we can execute the scripts and delete them By doing so, we will be able to achieve building a bit more secured webpages. Is'nt it?

Community
  • 1
  • 1
Mohammed Ashiq
  • 468
  • 4
  • 18
  • ` – Deepak Ingole Feb 28 '14 at 11:10
  • 2
    That script is executed on dom ready just once. Why do you need to remove it after its execution? – Fabrizio Calderan Feb 28 '14 at 11:11
  • What good reason could you have, to hide executed javascript from your visitors? – Lama Feb 28 '14 at 11:11
  • @Pilot.. I believe placing a script anywhere will not make a problem or its not wrong markup. Kindly look here [link](http://www.w3.org/TR/html4/interact/scripts.html#h-18.2.1) – Mohammed Ashiq Feb 28 '14 at 11:13
  • use a cookie to set that the request was done, always check this cookie to make the request – Felipe Pereira Feb 28 '14 at 11:15
  • Its just that people could see post information and a link which is completely for user analytics. which is kind of not exposable. – Mohammed Ashiq Feb 28 '14 at 11:15
  • people could easily see the post request by net panel of their browser. They could disable javascript and look at your source code so a client side solution is not suitable for this purpose. You need to invalidate the resource after the first call, for example using a server-side session – Fabrizio Calderan Feb 28 '14 at 11:17
  • - set a cookie param. - reload the page. - don't load the js this time. - use obfuscation to make your js hard to read Don't try to hide your javascript, it's a very dubious behavior. If it is only for user statistics, you could use PHP or build an java ee application. – Lama Feb 28 '14 at 11:18
  • Even if you were to remove the Javascript from the page the user could see the request and POST data in the network tab of their browser's debug tools or by viewing the page source. – rtcherry Feb 28 '14 at 11:19
  • @FabrizioCalderan true.. And Yes I am using a server-side check to invalidate after the first call. but my doubt was why not hide the javascript. – Mohammed Ashiq Feb 28 '14 at 11:23

2 Answers2

0

You could use plain Javascript to do this (not tested this)

var domNode = document.getElementById('delete_this');
var nodeParent = domNode.parentNode;
nodeParent.removeChild(domNode);

Not sure this is ideal but it should remove it completely from the DOM.

garryp
  • 5,508
  • 1
  • 29
  • 41
  • great idea @garryp. but remove() does almost the same thing what your code does. The script tag is easy to remove. But a remove() as in my function or nodeParent.removeChild(domNode) as in your code, doesnt work on success of an AJAX request. any reasons? – Mohammed Ashiq Apr 12 '14 at 22:29
  • Ah OK I see what you're trying to do. How about changing the on load event to$(document).ready(function(){ if(myFunc) myFunc(); }); rather than an anonymous inline function as you have currently, and put all that AJAX stuff in myFunc. You can then use delete myFunc; or myFunc = undefined; which should remove the function and the condition check for undefined should prevent errors. – garryp Apr 13 '14 at 00:33
0

This works :

<button>remove</button>

<script id="test">
$( "button" ).click(function() {
  $( "#test" ).remove();
  alert("removed " + ($( "#test" ).length == 0));
});
</script>

try it here: http://jsfiddle.net/4ungb/

So either you have a) other errors that prevent that script to complete or b) your criteria for testing deletion is not correct.

c-smile
  • 26,734
  • 7
  • 59
  • 86
  • the idea of deleting the script tag works. but for some reason, after the ajax request is fired, on getting the response (in done section) I used the remove() function. but it didnt remove. any reason for that. I have no errors in my page. The console looks clean. Right now, Just because I tried all possible options, and it didnt work out, I obfuscated javascript. but a solution for this will be a great lesson. – Mohammed Ashiq Apr 12 '14 at 22:25