Bug Tracker

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#14524 closed bug (notabug)

jQuery's Ajax interferes after adding jQuery script to an inner iframe

Reported by: anonymous Owned by: anonymous
Priority: undecided Milestone: None
Component: unfiled Version: 1.10.2
Keywords: Cc:
Blocked by: Blocking:

Description

In this example, attempting to jQuery.ajax or jQuery.get causes the outer window's jQuery variable to get replaced - the jQuery.dialog and any other plugins to jQuery are no longer there. (It happens in 1.9.1 and 1.10)

<html>
<head>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
</head>
<body>

Try and <button id="btn">load</button>
<iframe width=300 height=300></iframe>

<script>
"use strict";
jQuery('#btn').click(function(){
    var $ = jQuery;
    console.log(typeof jQuery('iframe').dialog);
    var doc = jQuery('iframe')[0].contentDocument;
    function insertscript(src) {
        var newscript = doc.createElement('script');
        newscript.setAttribute('src',src);
        doc.documentElement.appendChild(newscript);
    }
    insertscript('http://192.168.1.17/wordpress/wp-includes/js/jquery/jquery.js');
   
    //This breaks the jQuery:
    var test = $.get('http://192.168.1.17/wordpress/wp-includes/js/jquery/jquery.js',function(){
        //Now we know jQuery should be in the frame.
    });
   
    //So does this:
    //jQuery.ajax({url:'http://192.168.1.17/wordpress/wp-includes/js/jquery/jquery.js',cache:true,processData:false});
   
    console.log(typeof jQuery('iframe').dialog);
    window.setTimeout(function(){
        //jQuery is no longer the original jQuery object. Note the cached reference $().dialog does exist though.
        console.log('after awhile... dialog is ' + typeof jQuery('iframe').dialog);
    },3000)
    //jQuery.ajax({url:jqurl,cache:true,processData:false});
});
</script>
</body></html>

Change History (6)

comment:1 Changed 5 years ago by m_gol

Owner: set to anonymous
Status: newpending

What is this bug about? Why are you downloading a second copy of jQuery via the first one?

comment:2 Changed 5 years ago by anonymous

Status: pendingnew

Looks like the real bug here is that jQuery.ajax documentation mentions that the fetched data will be run, if it is a script, but jQuery.get has no mention of this. This should be a big bold item in my opinion, people will wonder why it's immediately running the script, especially if it only does so same-domain.

comment:3 Changed 5 years ago by dmethvin

Resolution: notabug
Status: newclosed

Notice that the first thing the jQuery.get)() docs do is tell you the equivalent jQuery.ajax() call. So, an astute reader could know that they need to read those docs. Or we could copy and duplicate a big chunk of docs into the ajax shorthand methods but that just makes maintenance harder.

comment:4 Changed 5 years ago by scottgonzalez

Or we could copy and duplicate a big chunk of docs into the ajax shorthand methods but that just makes maintenance harder.

We already have a solution for that problem: XML Includes.

comment:5 Changed 5 years ago by dmethvin

Now break up all the ajax options and discussion text into include-able parts and determine which are applicable for each of the .get/.post/.load shortcut methods.

comment:6 Changed 5 years ago by anonymous

Yeah that explains a lot

Note: See TracTickets for help on using tickets.