admin管理员组

文章数量:1302267

I have a Cocoa WebView that displays a local HTML file. I'm trying use jQuery.load() to load another local HTML file into a div. My JavaScript code goes something like this:

$("#mydiv").load("test.html");  // test.html is a local file inside the app's bundle

I don't see any exceptions but it will not load the html inside the div. I'm using exactly the same code for an iOS app, and it works fine. I've also tried using XMLHttpRequest() and I can see the responseText fine.

Any advice? Thanks in advance!

I have a Cocoa WebView that displays a local HTML file. I'm trying use jQuery.load() to load another local HTML file into a div. My JavaScript code goes something like this:

$("#mydiv").load("test.html");  // test.html is a local file inside the app's bundle

I don't see any exceptions but it will not load the html inside the div. I'm using exactly the same code for an iOS app, and it works fine. I've also tried using XMLHttpRequest() and I can see the responseText fine.

Any advice? Thanks in advance!

Share Improve this question asked Jul 17, 2013 at 23:47 ReinaldoReinaldo 4,6763 gold badges26 silver badges24 bronze badges 2
  • You could try putting a $(document).ready before the .load() to ensure the page is fully there before trying to load something new into it. Depending on load time I've had JS fail due to the targetted div not being there when it's trying to execute. – Peter Oram Commented Jul 17, 2013 at 23:53
  • Actually the code is being executed after the DOM has loaded. I'm thinking is something related to Cocoa's WebView as the same code runs without issues under iOS, Android and Windows. Thanks for the time. – Reinaldo Commented Jul 18, 2013 at 0:04
Add a ment  | 

3 Answers 3

Reset to default 3

A nice little work around just replace home.html with your local html file and content with your div id

document.getElementById("content").innerHTML='<object type="text/html" data="home.html" ></object>';

works in Chrome, Firefox, and IE enjoy!

I was able to get around the problem by using jQuery.ajax instead. Here's how I did it:

jQuery.ajax({ 
     url: "test.html", dataType: "html" 
}).done(function( responseHtml ) {
     $("#mydiv").html(responseHtml);
});

Hope it helps someone!

var $div = $('div#something');
$div.load("test.html");

本文标签: javascriptjQuery load is not loading a local html file inside a Cocoa WebViewStack Overflow