tags:

views:

1036

answers:

4

I tried the following:

$.load("Views/chatBox.html").appendTo('body') is not working!

TypeError: $.load is not a function

EDIT:The answer should be only one line of code,that's enough,I think

+1  A: 

Here you go:

<script type="text/javascript">
    $(function() {
     $("body").load("Views/chatBox.html");
    });
</script>
GaVrA
"Load HTML from a remote file and inject it into the DOM."So you just need some selector into which injection will be done. ;)Aka - yes, load does the work of appending.Check out that demo i made and posted and that tutorial, it helped me a lot.
GaVrA
Oh,no,it will replace the whole page with Views/chatBox.html,which is not what I desired.I need to append it to <body>.
Shore
Well what i am doing is having some container, in that demo it is #majice, and it is empty. Then i load external page into that. Makes sense?
GaVrA
Not exactly the same as what I desired.My case:1.no container 2.get html by ajax and append it to <body>
Shore
If there is a container,I can do this:$('#container').load(...).appendTo('body').Right?But I need to implement it without container.
Shore
A: 

I dont understand why placing container at the bottom of body and loading external page into that is not what you need?

What you can try is this:

<script type="text/javascript">
    $(function() {
        $("#container").load("Views/chatBox.html",function(){
      $(this).clone().appendTo("body").remove();
     });
    });
</script>

But im not 100% sure about this code... :)

GaVrA
So,Must jQuery bother some element to do ajax things?
Shore
Well you must always have some selector onto which something will be done. Some demo page or code would help in understanding what you want, but that previus answer is, i think, what you need. :)
GaVrA
A: 

When I tried out GaVrA's solution I found it could be even simpler:

<script type="text/javascript">
    $(function() {
        $("#container").load("external.html").appendTo("body");;        
    });
</script>

I guess appentTo automatically removes a previous instance? Perhaps I'm missing something here?

waffles