I have 2 HTML files (both in the same directory). How can I display content of one into another?
I can think of these ways:
- Create
XMLHTTPR开发者_JS百科equest
. Fetch the HTML file and display in appropriate location. - Use
iframes
What are others methods to diplay external HTML file?
You basically have three options to do this.
How do you even have proposed two:
- Using only HTML, you need to do it with
iframe
. Your user will need to make 2 http requests to load 2 html files. - Using only HTML and JavaScript, you also can work with
XMLHTTPRequest
or some JavaScript framework (like jQuery) to do that Ajax to you. Your users will need to make 2 http requests also, but SEO will be a problem in this case.
The third option, like others have suggested, is:
- Using a server side processing, like the suggested
PHP include
orASP.NET UserControl
. I think it's the best approach since the user will never know you have 2 files to show that page and also will need only 1 http request. And better for SEO crawlers too.
As an ASP.NET programmer, I personally like to have a base aspx
page and split its sections using the UserControls ascx
. The generated html remain clean in a single file.
You can also use server-side proxy. However, I'd personally use these iframes
If your host supports PHP then you can make the containing HTML file into a PHP file and include the other one with include()
. This is exactly the reason I learned PHP in the first place.
精彩评论