2

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!

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. 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. Commented Jul 18, 2013 at 0:04

3 Answers 3

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!

Sign up to request clarification or add additional context in comments.

Comments

2

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!

1 Comment

Doesn't work to me, same as doing .load on the jQuery object.
0
var $div = $('div#something');
$div.load("test.html");

1 Comment

I know it sounds like a simple answer, but it seems to me like my issue is related to Cocoa's WebViews and not jQuery. Your code is equivalent. Thanks anyways.

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.