1

i get condition where i need to request page with ajax. and i get HTML as result. parse those HTML result, and append parsed HTML content into current page.

let say this HTML content is the one requested :

<html>
<head></head>
<body>

<div id="main">
<script type="text/javascript" src="http://script.com/js/script.js"></script>
</div>

</body>
</html> 

How to parse #mainid selector (including javascript text) than append it on part of current HTML.

jQuery.ajax({
    url: requestedurl,
    type : "POST",
    dataType : "text",
    success: function(data) {
        console.log(jQuery(data).find("#main"));
        // than append
    }
});

is it better to not to parse using jquery, but regex? thank you very much for help.

ps : i try to load result using jquery, i get text node content, etc. unfortunately i don't know how to parse it. :(

2
  • "is it better to not to parse using jquery, but regex?" Absolutely not. Use jQuery. Have you tried jQuery(data).find("#main").appendTo("body");
    – nnnnnn
    Sep 11, 2012 at 3:01
  • then how to do that? when i load those html, i get only text node content, and javascript tag is gone :(
    – Jeg Bagus
    Sep 11, 2012 at 3:03

2 Answers 2

1

change your dataType from "text" to "html"

3
  • i empty object when try to execute console.log(jQuery(data).find("#main")); while dataType is HTML :(
    – Jeg Bagus
    Sep 11, 2012 at 3:06
  • conosle.log( $(data) ) and see what you get. It sounds like your html is wrong. If #main is the root element then there is no need for .find()
    – arhea
    Sep 11, 2012 at 3:16
  • result begin from <html> tag until </html> tag. result when i do loging is text node. i don't know what is this text node.
    – Jeg Bagus
    Sep 11, 2012 at 3:22
1

You should skip the textnode.

I got in the console [ Text, <div id=​"main">​</div>​, <script type=​"text/​javascript" src=​"http:​/​/​script.com/​js/​script.js"></script>​, Text]

so just get jquery(data)[1]

I tried the same thing as you with in test.html :

<html>
<head></head>
<body>

<div id="main">
<script type="text/javascript" src="http://script.com/js/script.js"></script>
</div>

</body>
</html>

and in another file

    <script type="text/javascript">
    $(document).ready(function(){
       jQuery.ajax({
        url: './test.html',
        type : "POST",
        dataType : "html",
        success: function(data) {
            console.log(jQuery(data)[1]); //Then i got the main div
            // than append
        }
    });
   });
    </script>
3
  • You might need to get jQuery(data)[2] too to append. Because it's the script that was inside the main div.
    – Elyx0
    Sep 11, 2012 at 3:44
  • well, its great. but i cannot find any <script> tag on the result.
    – Jeg Bagus
    Sep 11, 2012 at 3:58
  • What does console.log(jQuery(data)) outputs to the console, exactly ? I used the same html code as the one you provided so it's weird we don't have the same results
    – Elyx0
    Sep 11, 2012 at 4:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.