最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to read external html file and store it to string variable using jQuery? Showing Error - Stack Overflow

programmeradmin0浏览0评论

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

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

Share Improve this question edited Aug 20, 2016 at 9:11 Keerthivasan asked Aug 20, 2016 at 7:30 KeerthivasanKeerthivasan 1,6611 gold badge22 silver badges47 bronze badges 4
  • 3 Possible duplicate of How do I load html into a variable with jquery – fernando Commented Aug 20, 2016 at 7:38
  • @mariusz thank you my purpose similarly same as you shared, but its doesn't works – Keerthivasan 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. – Tomáš Hübelbauer Commented Aug 20, 2016 at 8:32
  • @Tomáš Hübelbauer Thank you i will try that – Keerthivasan Commented Aug 20, 2016 at 8:33
Add a comment  | 

3 Answers 3

Reset to default 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
});
this verion is working fine for me you have to close `);` 
$(document).ready(function()
    {
      $.get("leads/leads_custom_box.php", function(result)
       {
          alert(result);
       });
    });

using jquery

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论