Guybrush Threepwood
Guybrush Threepwood

Reputation: 151

How can I load external html files?

I need to make changes to an existing project that uses iFrames to dynamically load external html files. However, the html files are part of the same project, not external sites. If I'm not mistaken, iFrames are considered a terrible way of loading html content unless they are used to actually display external sites.

I have looked into web components but apparently, browser support is still spotty and unfortunately, I need to support IE9.

I know that the JQuery load() method can accomplish this but in my online research, that doesn't often come up as a proper way of loading external html in general and a proper replacement for iFrames in particular.

Is there a reason why JQuery shouldn't be used here and are there better and established ways of doing this? For example, I once saw a framework that dynamically built the interface out of separate "partials" but I don't remember which framework that was.

Upvotes: 1

Views: 125

Answers (2)

PiniH
PiniH

Reputation: 1941

It depends on the HTML -

If it's built like a full page - then iFrames are actually a decent solution - Also, iframes with the same origin let you have full control over the content from the parent, while still protecting CSS and JS variables which is pretty convenient.

If not - jQuery.load() will do the trick, you can also do it manually ofc, but if you already have jQuery in your project, just use it.

Upvotes: 3

Yehuda Schwartz
Yehuda Schwartz

Reputation: 3503

The load() function is almost always the best way to go, if you are encountering a specific issue using that function maybe you can share it?

Upvotes: 1

Related Questions