Bug Tracker

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#12485 closed bug (notabug)

html() include javascript twice

Reported by: maxou518 Owned by: maxou518
Priority: undecided Milestone: None
Component: unfiled Version: 1.8.1
Keywords: Cc:
Blocked by: Blocking:

Description

Hi,

Since v1.6 (also right for 1.8.1 version), if I do something like that :

<!DOCTYPE html>
<html>
        <head>
                <meta charset="UTF-8">
                <script language="javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js"></script>
                <script language="javascript" type="text/javascript">
                        $(document).ready(function() {
                                $.ajax({
                                        url: "testscript.txt",
                                        dataType: "html",
                                        success: function(html) {
                                                $("#receveur").html(html);
                                                hello();
                                        }
                                });
                        });
                </script>
        </head>
        <body>
                <div id="receveur">
                </div>
        </body>
</html>

... where testscript.txt contains :

<div>
        <script language="javascript" type="text/javascript">
                function hello() {
                        confirm("Hello");
                }
        </script>
        <h2>This is a title</h2>
</div>

... if I look with firebug, I find the function hello() implementation twice in the source. Is it normal or is it a bug ?

Change History (9)

comment:1 Changed 7 years ago by mikesherov

Owner: set to maxou518
Status: newpending

Thanks for contributing! Can you please provide a test case using a test like jsfiddle.net or jsbin.com or some other hosted solution so we can see the issue in action to determine if this is a bug? Thanks!

comment:2 Changed 7 years ago by maxou518

Status: pendingnew

To be more exact, I see that in Firefox 15.0 and his firebug.

I don't succeed to do ajax calling with jsfiddle or other. But, I've found where the problem comes from!

It's in the globalEval function of core.js (from 1.6 version) :

// Evaluates a script in a global context
// Workarounds based on findings by Jim Driscoll
// http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context
globalEval: function( data ) {
        if ( data && rnotwhite.test( data ) ) {
                // We use execScript on Internet Explorer
                // We use an anonymous function so that context is window
                // rather than jQuery in Firefox
                ( window.execScript || function( data ) {
                        window[ "eval" ].call( window, data );
                } )( data );
        }
},

Firefox doesn't like window[ "eval" ].call( window, data ); and put the content of data twice in the page. If I use window.eval(data);, it's ok.

comment:3 Changed 7 years ago by mikesherov

Status: newpending

I can not reproduce this: http://jsfiddle.net/5NcPp/1/ Please let me know what to change to show the bug happening.

comment:4 Changed 7 years ago by maxou518

Status: pendingnew

I see the bug on your page when I look with Firebug. It doesn't open the confirm twice (normal) but the hello implementation is in double.

Screenshot : http://img824.imageshack.us/img824/456/image1ksm.jpg

Last edited 7 years ago by maxou518 (previous) (diff)

comment:5 Changed 7 years ago by mikesherov

Status: newpending

I still can not reproduce... http://jsfiddle.net/5NcPp/2/ if it truly was in the source twice though, this new fiddle should print hi2. Does it? If not, I'm inclined to call this a bug in firebug and not jquery. Please let me know.

comment:6 Changed 7 years ago by maxou518

Status: pendingnew

Nop, it print hi1.

Screenshot : http://img163.imageshack.us/img163/2208/image2gg.jpg

You suppose that if there are 2 hello functions, it will go in each of them. But, maybe it just goes in the first hello function found and never in the other. So, it will never print hi2. I've also tried with a append() on the title and it is executed just one time.

Me, I suppose that Firefox (not Firebug) have a bug with window[ "eval" ].call( window, data ); in the version 15.0 (before, I don't know). If it's true, JQuery will need find something for this exception like it has ever done for IE and old chrome version (read http://weblogs.java.net/blog/driscoll/archive/2009/09/08/eval-javascript-global-context).

comment:7 Changed 7 years ago by mikesherov

Resolution: notabug
Status: newclosed

Sorry, but that not how JavaScript scoping works. If it was truly in there twice, it would have printed hi2. The only conclusion I can come to is that this isn't a bug in jQuery nor Firefox. Until we can prove this has some ill effect other than appearing incorrectly in the firebug console, I must close this as not a bug. Please feel free to reply if you can prove otherwise, and I'll gladly reopen! Thanks again for contributing!

comment:8 Changed 7 years ago by maxou518

So, why in this test : http://jsfiddle.net/vax82/ ... I just see "2!" and not "1...2!" ? He just call one hello function, not two.

comment:9 Changed 7 years ago by mikesherov

my point wasn't that the function would be called twice... My point was that "a" would've been incremented twice. Firefox's eval shows the hoisted source code, that's all that's happening.

Note: See TracTickets for help on using tickets.