6

I am trying to Dynamically Generating the Codes for Web Application
I Want to read External HTML File and store it to string variable in Javascript or Jquery ?
Is there any efficient way for this..?

HTML File - Object-text.html

<div class="text-object">
    <div class='text-area'>
        <span class='content'>
            <span class='title'> title</span><br/>
            <span class='address'>  address  </span>
        </span>
    </div>
    <div class='patch'></div>
</div>
<br class='float-clear'/>

JQUERY

$(document).ready(function()
{
  $.get("templates/object-text.html", function(html_string)
   {
      alert(html_string);  // this is not Working
   });
});

Showing Error in Console

Junk After Docuement Element

awaiting responses.
Thanks in Advance

4
  • 3
    Possible duplicate of How do I load html into a variable with jquery Commented Aug 20, 2016 at 7:38
  • @mariusz thank you my purpose similarly same as you shared, but its doesn't works Commented Aug 20, 2016 at 7:55
  • 1
    Are the contents of the HTML file okay? Can you check the Network tab in the developer tools and inspect the Response tab of the XHR request to see if the file isn't mangled? Assuming Junk after document element is the error message you are getting, maybe XHR isn't the issue here, instead jQuery maybe tries parsing XML and fails? Check api.jquery.com/jquery.get dataType default is intelligent guess, try setting it to string. Commented Aug 20, 2016 at 8:32
  • @Tomáš Hübelbauer Thank you i will try that Commented Aug 20, 2016 at 8:33

3 Answers 3

11

Changed JQUERY Code

i've added the extra arguement to the function call $.get() to specify the html content

$(document).ready(function()
{
  $.get("templates/object-text.html", function(html_string)
   {
      alert(html_string); 
   },'html');    // this is the change now its working
});
Sign up to request clarification or add additional context in comments.

1 Comment

Today 3 Dec 2022 and it works. you saved me a big trobule.
1
this verion is working fine for me you have to close `);` 
$(document).ready(function()
    {
      $.get("leads/leads_custom_box.php", function(result)
       {
          alert(result);
       });
    });

8 Comments

just add a closing bracket for $.get( ); it will work
What you getting the output..? HTML Code or what else..?
i already closed that in my local file.. i am getting object XMLDocument
what is in your file you want to include what content it posses
$(document).ready(function() { $.get("templates/epc-object-text.html", function(result) { alert(result); }); });
|
1

using jquery

$.get("http://localhost/something/something.html", function (result) {
     html = result;
     text = $(result).text();
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.