开发者

How can i include a html file in a div?

开发者 https://www.devze.com 2023-02-16 11:28 出处:网络
A lot of an开发者_Go百科swers to my previous question have been suggestions that i stop using frames and iframes and use DIV. now my question is how to do you include a html file inside a DIV. it does

A lot of an开发者_Go百科swers to my previous question have been suggestions that i stop using frames and iframes and use DIV. now my question is how to do you include a html file inside a DIV. it doesn't look straight forward.


Should be very easy and simple.

Just load jQuery on your page and use the load() method and pass in the file you want to load.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() { // this runs as soon as the page is ready (DOM is loaded)
   $("div") // selecting "div" (you can also select the element by its id or class like in css )
   .load("path/to/your/file.html") // load in the file specified
  });
</script>


Start using a server-side language instead of just html files.

PHP's include() function

include("myfile.php");

.NET example:

string file = Server.MapPath("myfile.txt");
if(System.IO.File.Exists(file)){
   Response.Write(System.IO.File.ReadAllText(file));
}

Keep in mind using javascript to load content gives you no SEO benefit. Using a server-side language will open the door for you to make your development go quicker anyway. You'll see the benefits immediately. You'll start using an include to pull in your header, footer, and probably content. Benefits would be that instead of digging through 100 html files to change 1 item in your header navigation, you'll only do it once and it'll immediately work through the rest of the site.


Generally you would use some form of include mechanism or template language.

My preference would be to use Template-Toolkit and the WRAPPER directive to wrap the content with the page template.

A div, however, is not a substitute for a frame. Use the markup most semantically appropriate for the content. Div is the last resort, for when there is an element with good semantics. (It is still a lot better than an element with the wrong semantics though).


To my ears, the suggestion to stop using frames/iframes seems to imply switching to Ajax.

0

精彩评论

暂无评论...
验证码 换一张
取 消