views:

71

answers:

1

I have an html page with (among other things) a Unity3D window. I would like to replace everything on the page without causing the Unity window to reload. I have tried the following jquery-tastic

   function replaceSurround(keepElem, newElem)
    {
        keepElem.siblings().remove();
        keepElem.prepend(newElem.prevAll());
        keepElem.append(newElem.nextAll());

        var keepParent = keepElem.parent();
        var newParent = newElem.parent();
        if (keepParent && newParent)
        {
            replaceSurround(keepParent, newParent);
        }
    }

where keepElem is an element in the original document and newElem is the corresponding element in the new document, but it did not work very well.

A: 

Here is what I've got, it seems to work...

jQuery.fn.rewrap = function(newWrap){
    var $parent = jQuery(this).parent();
    var $clone = jQuery(this).siblings().clone()
    var $newParent = $clone.wrap(newWrap).parent().clone();
    $parent.replaceWith($newParent);
}


$('#header').rewrap('<div class="container" style="background-color:blue;" />');

I tested it on the Stackoverflow website. One small problem though, it seems to be refiring some onX events...?

[edit]

On second thought, that is not what you meant at all....

Can't you just do something like:

$('#result').load('ajax/test.html #result');

?

SeanJA
My main problem is that I need to reload a bunch of javascript files (I'm trying to change the locale of my site without reloading the unity window, and all my script files have been compiled for each locale separately), and as those scripts can do just about when reloaded I don't think I'll have much luck, and I'll have to actually reload the entire page. Thanks for the effort though :)
KvanteTore