niko
niko

Reputation: 9393

.load() jquery documentation

I want to load a html page to my container when user submits some thing.

I have used

$("container").load("page_broken.html",function(responseTxt,statusTxt,xhr){
            if(statusTxt=="success") alert((responseTxt));
        });`

but page_broken.html has <!doctype> <html> <head>(linking to css files and jquery) <body>and notice my original html file, already has this tags and now when I load my container with this html file

Iam worried these tags gets wrapped inside my container.

http://api.jquery.com/load/ Here is a link which says

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document. During this process, browsers often filter elements from the document such as <html>, <title>, or <head> elements. As a result, the elements retrieved by .load() may not be exactly the same as if the document were retrieved directly by the browser.

but when i used alert, to see the response,it has <!doctype> <body> and <html> tags.

So what would be the solution, is the documentation right? It may return the tags but it may not load? Could anyone explain me this clearly. Thanks

Upvotes: 3

Views: 60

Answers (1)

Kevin B
Kevin B

Reputation: 95031

The first parameter for .load's success callback is the raw html that was returned from your server. If your server returned doctype head title etc, then that raw html will contain it too. jQuery parses this html and inserts it into the target document using innerHTML. innerHTML will strip those tags from the content because they are invalid at that point.

Upvotes: 1

Related Questions