Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving a DOM element containing a dynamically created script tag

I'm using the Fish Gadget (http://abowman.com/google-modules/fish/) within a wiki based CMS, and need to reposition the gadget from one HTML element to another. (Note: the fish gadget is an example -- the problem occurs with other gadgets as well.)

If I directly reposition the gadget using the gadgets base class "ig_reset", then everything works. If I try to reposition using a surrounding wrapper, then the iframe used by the gadget seems to take over. Unfortunately I need the flexibility of repositioning using the surrounding wrapper.

This seems to have something to do with moving a SCRIPT tag around in the DOM. The gadget dynamically creates a script and a style tag. If I remove the dynamically created script tag from the DOM, and then reposition the wrapper to another location in the DOM, all works well. If I try to move the script tag to another DOM element then the original problem occurs. So moving a script tag around the DOM seems to be the cause -- regardless of when the move occurs (even post-load).

I'd like to understand what is happening here to cause the frame to take over the page, and also find a better solution than removing the dynamically created script tag.

I put a test up here: http://solidgone.com/jquery/google-gadget.html -- the demo uses jQuery, but I don't think this is related to jQuery...

like image 932
DaveG Avatar asked Nov 25 '08 18:11

DaveG


People also ask

How do I grab an element from a DOM?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable. Logging demoId to the console will return our entire HTML element.

Can JavaScript modify DOM?

The HTML DOM allows JavaScript to change the content of HTML elements.

Which attribute is used by scripts to identify an element in a document?

The type attribute must be specified for each SCRIPT element instance in a document. The value of the type attribute for a SCRIPT element overrides the default scripting language for that element. In this example, we declare the default scripting language to be "text/tcl".

Which of the following attribute Specifies language used to develop the script?

type: This attribute specifies the scripting language of the element's contents and overrides the default scripting language. The scripting language is specified as a content type (e.g., "text/javascript"). Authors must supply a value for this attribute. There is no default value for this attribute.


1 Answers

Whenever you append a script element into a page, using jQuery, it will attempt to execute it. Thus when you move ig_reset (which is only a table - no script) it works without issue. When you try to move the wrapper - which contains the script - the script is moved and re-executed.

We're working to fix this re-execution issue in jQuery core but for the time being that's what is going on here.

like image 126
John Resig Avatar answered Oct 12 '22 05:10

John Resig