Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move an applet without reloading it

I want to move an applet from a div element to another div element without reloading it. I don't want to use absolute positioning. Is there a way to do this ?

I try this but it's not working:

<html>
    <head>
    <script type="text/javascript">
        function toDiv1() {
            var appletElt = document.getElementById('myApplet');
            document.getElementById('div1').appendChild(appletElt);
        }

        function toDiv2() {
            var appletElt = document.getElementById('myApplet');
            document.getElementById('div2').appendChild(appletElt);
        }
    </script>
    </head>
    <body>
        <div id ="myApplet">
            <applet width="200" height="200" 
                codebase="http://mainline.brynmawr.edu/Courses/cs110/spring2002/Applets/Smiley/"
                code="Smiley.class"
                name="Smiley">
            </applet>
        </div>
        <div id="div1"></div>
        <div id="div2"></div>
        <div>
            <button onclick="toDiv1()">toDiv1</button>
            <button onclick="toDiv2()">toDiv2</button>
        </div>
    </body>
</html>

You can try your answer on this fiddle


The solution proposed by @goldenparrot with outerHTML works but not on all the browser (at least Firefox ESR).

As stated in the @kritzikratzi answer and in the @biziclop answer, there are known problems on firefox with the reloading of iframe, flash object, plugin in firefox.

I think (but i am not sure) that the sole solution is to use absolute positionning (@cuzzea). However, i asked this question because i wanted to find another way ("I don't want to use absolute positioning"). This is why i will award no answer.

Thanks for your contributions.

like image 226
gontard Avatar asked Aug 22 '12 11:08

gontard


4 Answers

I don't think that what you are looking for is posible, but if you want to use positioning you can try this:

http://fiddle.jshell.net/JRZXF/12/

HTML:

<div style="position:relative">
    <div>
        To test your code, make the smiley sad, then move it to another div. It must be still sad.    
    </div>

    <br>
    <div id ="myApplet">
        <applet width="200" height="200"
            codebase="http://mainline.brynmawr.edu/Courses/cs110/spring2002/Applets/Smiley/"
            code="Smiley.class"
            name="Smiley">
        </applet>
    </div>
    <div id="div1"></div>
    <br>
    <div id="div2"></div>
    <br>
    <div>
        <button id="button1">toDiv1</button>
        <button id="button2">toDiv2</button>
    </div> 
​</div>​​​​​​​​​​​​​​

Javascript:

function toDiv1() {
    $('#div1,#div2').empty();
    var el = $('#myApplet'),
        div = $('#div1'),
        clone_div = $('<div>');
    var width = 200,
        height = 200;
    clone_div.css({width:width,height:height});
    div.append(clone_div);
    el.css({
        position:'absolute'});
    el.css({
        left:clone_div.offset().left,
        top:clone_div.offset().top
    });
}

function toDiv2() {
    $('#div1,#div2').empty();
    var el = $('#myApplet'),
        div = $('#div2'),
        clone_div = $('<div>');
    var width = 200,
        height = 200;
    clone_div.css({width:width,height:height});
    div.append(clone_div);
    el.css({
        position:'absolute'});
    el.css({
        left:clone_div.offset().left,
        top:clone_div.offset().top
    });
}

$('#button1').click(toDiv1);
$('#button2').click(toDiv2);​

Of course this code can be improved a lot and made to work nicer. For this example to work you need all the nested divs to have position relative. I also gave the width and height 200 but you can make it dinamic, something like:

var width = el.width()

or

var width = el.children('applet').attr('width');
like image 198
cuzzea Avatar answered Nov 20 '22 03:11

cuzzea


Now, I have come up with a brilliant( :D ) solution using outerHTML:

function toDiv1() {

  var appletElt = document.getElementById('myApplet');

  var Div1 = document.getElementById('div1');

  var parts = Div1.outerHTML.split(">"+Div1.innerHTML+"<");

  // newHTML can also be appletElt.outerHTML
  var newHTML = Div1.innerHTML + appletElt.outerHTML;

  // Deleting Div1 because you will have multiple divs on page with id='div1'!
  Div1.parentNode.removeChild(Div1);

  appletElt.outerHTML = parts[0] + newHTML + parts[1];

}

IMPORTANT: First, your applet tag must close too. I mean like: <applet></applet>

Also Important:

There are some things you should know about the solution.

  1. The solution only works the first time. That is, you can move it to div1 or div2.
  2. The HTML WILL break if you click two buttons or one button more than once.
  3. You have to write toDiv2() function by replacing words (Div1 && div1) in toDiv1() with words (Div2 && div2).

I am posting this even if isn't the best solution because, I believe you can figure out how to make it work even for multiple clicks if you are on right track. And, you should be, after seeing this answer.


EDIT:

Here is the basic thing solution tries to do:

appendChild will create a new element and append it. And if a new element is created, it's not what you want.

outerHTML is a property of HTMLElement that you can change. For demonstration of outerHTML see how this fiddle works.

like image 45
Prasanth Avatar answered Nov 20 '22 03:11

Prasanth


might be a case of "simply out of luck", googling a bit shows there are many browser bugs that sound related:

can't move iframe in dom without reloading:

  • https://bugzilla.mozilla.org/show_bug.cgi?id=254144 (open)
  • https://bugs.webkit.org/show_bug.cgi?id=13574 (open)

can't move plugin in dom

  • https://bugzilla.mozilla.org/show_bug.cgi?id=90268 (fixed in FF13)

your best shot might be playing around with element.adoptNode(...) and the already mentioned outerHtml. good luck :)

(would love to test, but because of all the java mess on os x i can't even run the java plugin atm)

like image 41
kritzikratzi Avatar answered Nov 20 '22 01:11

kritzikratzi


I tried the following on IE8

applet=document.getElementById('myapplet');
applet.outerHTML='<div id="my_div">'+applet.outerHTML+'</div>';

and this causes the applet to be stopped, destroyed, initted and started again. So this seems to demonstrates that outerHTML does not work. Also, outerHTML on Firefox 13 puts a newline as the first character returned. Just beware when using it to search for the content returned by outerHTML.

Also, the original post by gontard seems to work on Firefox 13 (without using outerHTML or other tricks)

like image 1
mario Avatar answered Nov 20 '22 03:11

mario