Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Browser crashes with below code.What is wrong in below code.

Reply
Thread Tools

Browser crashes with below code.What is wrong in below code.

 
 
Evertjan.
Guest
Posts: n/a
 
      12-07-2011
kiran wrote on 07 dec 2011 in comp.lang.javascript:

> They feel its code issue and not an issue with jQuery.
>


So jQuery is not just code?

That it is code-to-be-avoided-at-all-cost is clear to me.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      12-07-2011
On Dec 6, 11:04*pm, Scott Sauyet <(E-Mail Removed)> wrote:
> RobG wrote:
> > On Dec 6, 1:06*am, kiran <(E-Mail Removed)> wrote:

>
> >> Thanks Rob and everyone for confirming this.I will bring this to the notice of jQuery team and see what they have to say on this.

>
> > Some thoughts from jQuery-o-philes:

>
> > <URL:http://stackoverflow.com/questions/8...-causes-browse...

>
> Doctor, it hurts when I do this...


When "this" is something that should not hurt, it seems reasonable to
bring it to the attention of someone who might be able to explain why
does it hurt, if not fix it.

Can you explain why browsers do not execute script elements in cloned
nodes when using the DOM cloneNode method, but when using jQuery's
clone function, they do?


--
Rob
 
Reply With Quote
 
 
 
 
Scott Sauyet
Guest
Posts: n/a
 
      12-07-2011
RobG wrote:
> On Dec 6, 11:04*pm, Scott Sauyet <(E-Mail Removed)> wrote:
>> RobG wrote:
>>> Some thoughts from jQuery-o-philes:
>>> <URL:http://stackoverflow.com/questions/8...-causes-browse...

>
>> Doctor, it hurts when I do this...

>
> When "this" is something that should not hurt, it seems reasonable to
> bring it to the attention of someone who might be able to explain why
> does it hurt, if not fix it.


Sorry, I should have put quotes around that. It was my attempt to
summarize the responses from the jQuery community, not my own view.


> Can you explain why browsers do not execute script elements in cloned
> nodes when using the DOM cloneNode method, but when using jQuery's
> clone function, they do?


Yes.

But I'm not sure I want to, because it might make it sound as though I
agree with jQuery's decision on this. So, with the caveat in mind
that I don't agree with their decision, let me first point out that
jQuery is just Javascript. The DOM `cloneNode` method is still
available. So they have to decide whether their method is just a thin
wrapper around `cloneNode` or whether it is meant to do something
different. They have chosen to do more. For one thing, jQuery copies
over not just the DOM material, but also, optionally, copies the event
handlers on the node and, with another option, on its descendents. I
like this decision they made, but it could also lead to another
question about "Why does this jQuery example differ from this parallel
DOM one?"

They also decided to run the scripts that were included in the copy
(presumably only after the copy is attached to the DOM, although I
haven't looked closely.) This allows for the style of dynamic
generation that puts SCRIPT markup immediately after the markup for
elements on which it will operate, something like:

<div id="template">
<div class="xyz">...</div>
<script>process($(".xzy:last")[0]);</script>
</div>

$("#template").clone(true).appendTo(someContainer) ;

With this, whatever server-side decisions that were made about how to
handle this templated content are duplicated when it's cloned.

This is not a style of development I like, and I don't really see a
reason for jQuery to cater to what I see as poor practice, but I do
understand it, and I've been on projects that used it heavily.

So I suppose I've only answered half the question. This is why jQuery
does run the scripts when cloning. As to why browsers don't do so in
cloneNode, I can't really say.


I also would like to point out that this code:

<div>div
<script>
var el = document.getElementsByTagName('div')[0];
document.body.appendChild(el.cloneNode(true));
</script>
</div>

is not the most direct port of the jQuery code:

<div>
<script>$('div').clone().appendTo('body');</script>
</div>


This naive port would be semantically a little closer:

<div>div
<script>
var els = document.getElementsByTagName('div');
for (var i = 0; i < els.length; i++) {
document.body.appendChild(els[i].cloneNode(true));
}
</script>
</div>

Of course because of the live NodeList, that one also has an infinite
loop problem. It's a different issue than in the jQuery code, but
it's still a real issue.

-- Scott
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft's Internet Explorer browser falls below 50% of worldwidemarket for first time Sweetpea NZ Computing 6 10-12-2010 08:07 AM
James Gosling the Creator of EMACS and JAVA - leaves ORACLE - Butthen reports started coming in of odd failures. Systems would crashstrangely. We'd get crashes in applications. All applications. Crashes in thekernel. small Pox C Programming 2 07-22-2010 10:07 PM
What is wrong in below simple code snippet? doublemaster007@gmail.com C++ 14 12-07-2009 09:38 AM
Dotnetnuke, clean install -> wrong login crashes default application pool Ronald ASP .Net 3 10-23-2006 03:59 PM
Could someone scan me a picture of the below? (Read Below) starlightvoyager@yahoo.com DVD Video 1 08-28-2006 05:42 AM



Advertisments