views:

67

answers:

3

Does anyone know if you can remove head elements on a button click and how to do it with jQuery?

I am trying to get rid of certain script tags from the html head when a button is clicked. For instance. I have 1 screen view with a slideshow controlled by an external javascript file. When I click on a button "Click to get rid of this elements JS" I want to remove the external javascript path from the HTML Head.

Any ideas. Have been at this thing for a week or so.

+1  A: 

I don't think its a good Idea to remove Entire HEAD Element. 'Cause your Page may contain some more Elements (i.e., title, style..) which are appended to Head Element. If you want to remove a particular script Element do something like

$(function() {
  $('input[type=button]').click(function() {
      $('script[src=path/file.js]').remove();
  });
});

Edit :

var flag = false;
function breakTheCode() {
    if(!flag) {
        //run your code
    }else return;
 }

$(function() {
  $('input[type=button]').click(function() {
      flag = true; //flag is set, so we no more using/ running your code
      breakTheCode(); //call you function/method
  });
});
Ninja Dude
does this unset variables and functions defined in path/file.js?
akellehe
@akellehe Forgive me :) I haven't understood ur comment. you mean, what if the above code is written in `path/file.js` huh ??
Ninja Dude
sorry, yes, if you set a variable or a function in the file located at path/file.js and you include it as a script source in the document head; does this function you suggest unset the variables and functions in that file?
akellehe
@akellehe Why don't you run this test on your own? As far as I know, in most browsers external resources are kept in memory, regardless of what happens to them in the DOM. Set a variable in some external script, run your unload function posted by Avinash, then open up your console and do alert(myVariable). I'm betting your variable will alert, but you should test it out.
treeface
@treeface Thanks for reminding about browsers cache. So my answer doesn't gonna have any impact :) +1
Ninja Dude
Umm well that kinda worked. Using Firebug is showed this worked, however, it did not actually stop the script. I pressed the button a few times and the slideshow still was working.Any ideas?
carlos
@treeface can you explain more? Like you mentioned I tried Avinash's idea but yeh did not quite work. So is this possible to remove the script tags from the DOM as well as force the script to stop working?
carlos
@carols As treeface Said, when your content is downloded, all your code is stored in browsers memory. Though you'd removed the Element Dynamically, still the browsers cache contains your file content i.e., your variables, functions.. As far as I know it is difficult to achieve your requirement in any of the browser which supports caching of files/data and I admit that my answer doesn't gonna satisfy your need, sorry =)
Ninja Dude
@Avinash Good answer. In reality, it's practically **impossible** to achieve this in modern browsers, nor do I really see a reason why you should need it. If @carlos wishes to manage memory in the browser, he can simply unset complex variables and not worry so much about the other ones. The biggest issue with javascript (outside of more complex scripts like those used in the canvas element) is usually the file size, and once the user has downloaded that, you can't un-download it to make your page go faster (unless, of course, you know how to alter the forward progress of time).
treeface
More on this point....if you know the name of the global object that is (presumably) the main controller of your plugin scripts, you could simply set that to null and unload it from memory, but you can't get rid of the script itself.
treeface
OK then how would you remove javascript NOT externally. What if it is within the DOM not and external file. Can I target it then?
carlos
@carlos you got understand, it is impossible to remove the already stored content in browser's memory. If you still want it to be done, i suggest you to wrap you code in a `function`, when the user clicks the buttons just set a flag(condition) as false/true and return from the function by not executing the block of code.
Ninja Dude
@Avinash Thanks for the help. I appreciate it. I was just trying to see if there was something. But it looks like modern browsers can not do this. Might be a server side thing.Thanks all. I will consider this closed.
carlos
+1  A: 

You can do this sort of thing using javascript, sure, but before you do it, you might want to ask yourself again why. Here's a link describing how to do it in pure javascript with a jquery example provided by the other answerer:

http://www.javascriptkit.com/javatutors/loadjavascriptcss2.shtml

But try to keep in mind that most modern browsers will keep these external resources in memory for at least as long as the page is open. Therefore, you won't really be doing much.

treeface
@treeface +1 For the link :)
Ninja Dude
A: 

You can add an id to a script element then remove that ID:

<script type="text/javascript" src="init.js" id="initJs" ></script>

<span id="removeScript"></span>

$('#removeScript').click(function() {
     $('#initJs').remove();
});
j-man86