-3

For example:

<div class='blah'>
    <script>
        $('.blah').remove();
    </script>
</div>

Edit:

Okay, I got a lot of down-voting for this. I would rather know if this is a good idea, but since that is subjective let me ask: how does javascript handle a function that deletes itself?

astex
  • 1,007
  • 9
  • 26
  • 9
    Have you tried it? Did it work? – Steve Mar 12 '13 at 21:22
  • 2
    `html` elements do not contain `functions` – Brad M Mar 12 '13 at 21:22
  • 2
    I'm afraid the universe will implode. – astex Mar 12 '13 at 21:22
  • 4
    This seems like something you could just try and find out. – Shmiddty Mar 12 '13 at 21:22
  • 1
    You just blew my mind good sir – Andy Jones Mar 12 '13 at 21:23
  • @BradM 1. It's jQuery 2. Only use `code` for... Well, code. – tckmn Mar 12 '13 at 21:24
  • So, what's the point ? – pero Mar 12 '13 at 21:27
  • This can be useful if the script is meant to replace the containing html with something else (i.e data). I realize that it is roughly equivalent to `$('blah').load(function() { //Do stuff });` somewhere else. I just thought it was neat. – astex Mar 12 '13 at 21:29
  • @astex Absolutely not. The script itself would not replace the containing HTML. The script doesn't care about the HTML. That's why it's enclosed in it's own script tags. – David L Mar 12 '13 at 21:30
  • 1
    Actually... it kinda brings an interesting point. How the JS parsing is implemented? I suppose JS is parsed and put in memory separately from DOM and then executed ? Therefore it should be possible to play around with script DOM elements. Funny :) – pero Mar 12 '13 at 21:35
  • So what happens to any functions in that script tag? Do they stay in memory until the end of days? Do they finish executing then disappear? – astex Mar 12 '13 at 21:36
  • 1
    Maybe this can help: http://stackoverflow.com/questions/1096907/do-browsers-parse-javascript-on-every-page-load look at most voted answer. – pero Mar 12 '13 at 21:38
  • I suppose if you define function in global scope then it's stays in memory until you close windows with this page. If you define in private scope or run it straight like: (function() { .. })(); then it should be removed... but those are only my imaginations ;-) – mariozski Mar 12 '13 at 21:38
  • I think this merits additional study. – astex Mar 12 '13 at 21:40

4 Answers4

3

Yes you can -> http://jsbin.com/ososuh/1

What's more you can define function, which you'll invoke on later time. I suppose it's added on parsing time to memory and it can be invoked later on. Haven't tried it before but must admit it's quite funny ;-)

mariozski
  • 1,114
  • 1
  • 7
  • 20
1

First of all, you should never be placing your javascript inline like this. It's bad practice and terrible for web optimization. Second of all, sure, why not?

If you are bound to the element in some way, you can still removed the HTML, though it would not remove the actual script function itself.

$('.blah').bind('click', function () {
    $(this).remove();
}
David L
  • 28,075
  • 7
  • 54
  • 84
0

Yes. In case of functions that doesn't mean they will be undefined after that, but you can very freely modify the DOM from JavaScript.

Squeezy
  • 455
  • 4
  • 11
  • So the code within a ` – Dai Mar 12 '13 at 21:25
  • If you redefine it, it will hold the new definition. It doesn't matter where this sources from, as long as it affects the same scope. – Squeezy Mar 12 '13 at 21:27
0

Look at the answer at Do browsers parse javascript on every page load?

For example:

Chrome : V8 Engine

V8 has a compilation cache. This stores compiled JavaScript using a hash of the source for up to 5 garbage collections. This means that two identical pieces of source code will share a cache entry in memory regardless of how they were included. This cache is not cleared when pages are reloaded.

So, if you hit F5 in Chrome it will detect (via hashing of text of script tag content) that it already has this piece of JavaScript code parsed and compiled into native code. So it will just execute it.

As this already compiled piece of JS code is separated from DOM (in different part of memory) it can manipulate also the DOM element that represents itself. At least that is how I understand the working.

Community
  • 1
  • 1
pero
  • 4,021
  • 24
  • 27