Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>jQuery does not store the data with the element itself but in <code>jQuery.cache</code>.</p> <p>In jQuery code you have this part:</p> <pre><code>jQuery.expando: "jQuery" + ( core_version + Math.random() ).replace( /\D/g, "" ) </code></pre> <p>As you can see for each jQuery load a unique expando is created.</p> <p>The expando is used as property to store an identifier with the DOM element.</p> <p>When you store data with <code>.data(key,value)</code> in an element jQuery does the following steps:</p> <ol> <li>check if there is an <code>id</code> stored with the element in <code>element[jQuery.expando]</code> if not it creates an unique <code>id</code>.</li> <li>check if there is an entry with <code>jQuery.cache[id]</code> if not create an empty object for storing the data for the element there.</li> </ol> <p>So if you call <code>.data(key,value)</code> the data is stored in the window the jQuery instance you use is defined in.</p> <p>If you have a jQuery object in the <code>parent</code> and one in the <code>iframe</code> these have two different <code>expandos</code> because of the random number. And if you call <code>.data()</code> from the parents jQuery object on an element of the iframe, the <code>expando</code> of the parent is used and the data is stored within the parent. If you then use the iframes jQuery and then call <code>.data</code>on the same element as before the iframe's jQuery would not find any data because it has on the one hand a different <code>expando</code> and on the other hand the data is stored in the parent window.</p> <p>So if you want to set data in the iframe you should use the iframes jQuery object. <code>$('iframe')[0].contentWindow.jQuery("body").data( ... )</code> to set the data, then it is possible to retrive that data from inside of the iframe again, because then you use the same jQuery object for setting and reading the data.</p> <p><strong>EDIT</strong> One additional and important note. Because the data is stored with the used jQuery instance, you should not use jQuery to store data in another context. JQuery has a cleanup methode that is called when you remove elements with jQuery which removes event listeners and removes the data from <code>jQuery.cache</code>. But if you use jQuery to store data for an element that is in another context this cleanup method will fail ( e.g. if you load another page in the iframe). So the data then will only be freed if you reload the parent.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload