最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reuse elements of HTML - Stack Overflow

programmeradmin0浏览0评论

I'm writing a static web site that uses JQuery to make some AJAX calls to a RESTful API and populate the page with data.

The site functions correctly (and quickly), everything is good.

As I extend the site and add additional pages, I'm noticing that I'm duplicating certain regions on every page.

For instance, each page shares a common header element.

<header>...Some non-trivial content...</header>

Rather than repeat this definition on each page is there some mechanism, by which, I can define this section once and include it in each document.

Remember that the pages must be served statically but any standard complaint browser functionality can be utilised.

Is there a good way to do this, and what is it or, will I have to abandon DRY principles for this aspect of my client side code?

I'm writing a static web site that uses JQuery to make some AJAX calls to a RESTful API and populate the page with data.

The site functions correctly (and quickly), everything is good.

As I extend the site and add additional pages, I'm noticing that I'm duplicating certain regions on every page.

For instance, each page shares a common header element.

<header>...Some non-trivial content...</header>

Rather than repeat this definition on each page is there some mechanism, by which, I can define this section once and include it in each document.

Remember that the pages must be served statically but any standard complaint browser functionality can be utilised.

Is there a good way to do this, and what is it or, will I have to abandon DRY principles for this aspect of my client side code?

Share Improve this question asked Aug 30, 2013 at 13:39 JodrellJodrell 35.7k6 gold badges92 silver badges128 bronze badges 7
  • Any restrictions on putting the header in php and including it in the pages ? – Patsy Issa Commented Aug 30, 2013 at 13:40
  • Any view engines being used? Or just straight HTML? – tymeJV Commented Aug 30, 2013 at 13:41
  • Take a look at this: stackoverflow.com/questions/8988855/… – Alvaro Commented Aug 30, 2013 at 13:41
  • Any objection to using Jade/Stylus for development (or something of that ilk) to dynamically generate your files? – kunalbhat Commented Aug 30, 2013 at 13:41
  • 5 $('#content').load('reusablefile.htm'); ? That's how I do it. – Johan Commented Aug 30, 2013 at 13:41
 |  Show 2 more comments

7 Answers 7

Reset to default 7

There are definitely some ways to achieve this. You could either do it using some features of your server-side language that allow you to include the content of a page in another page, or if you do not have any server-side technology, you could simply put that code in its own html document and load its content using AJAX.

In jQuery it could look like:

$('#header').load('header.html');

However, if the content isn't static for all pages, you could always define a JS module that would be responsible for rendering this header. Your module could make use of a client-side templating engine, like Mustache, Handlebars, etc. However you do not have to use any of these.

Here's a simple example:

DEMO

//in somefile.js, please note that you should namespace your modules
var Header = {
    //default config
    config: {
        el: '#header',
        title: 'Some title'
    },
    
    init: function (config) {
        var cfg = this.config = $.extend({}, this.config, config);
        
        $(cfg.el).html('<h1>' + cfg.title + '</h1>');
    }
};

$(function () {
    Object.create(Header).init({
        title: 'Some other title'
    });
    
    Object.create(Header).init({
        el: '#header1',
        title: 'Yeah'
    });
});

As I mentioned in the comment, this is how I do it:

main.html

<!DOCTYPE HTML>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Main page</title>
        <sript src="http://code.jquery.com/jquery-latest.js"></script>
        <script>
             $(function(){
                  $('#commonsection').load('reusablefile.htm');

                  // which is eqvivalent to:
                  //
                  // $.ajax({
                  //     url: 'reusablefile.htm',
                  //     dataType: 'html',
                  //     success: function(data){
                  //         $('#commonsection').html(data);
                  //     }
                  // });
             });
        </script>
     </head>
     <body>
         <div id="commonsection"></div>
     </body>
</html>

reusablefile.html:

<script>
    (function($){ //separate scope to keep everything "private" for each module
        //do additional javascript if required
    })(jQuery);
</script>
<p>...Some non-trivial content...</p>

You could use jQuery's ajax as to load the header file. In each file you could load the html like so:

$('#header').load('header.html');

Since you're already using AJAX calls to populate your site with data, you could do the same for the common regions.

Just store the HTML for those regions in a separate file and load it in the page with AJAX. Also, you can work with caching using the Cache-Control headers on that file so you don't reload the entire content from the server with each page load.

If you're using straight HTML, you could do it with a SSI include command or by creating a template page and including it in jQuery. Both of these links might help you

Include another HTML file in a HTML file and http://www.htmlgoodies.com/beyond/webmaster/article.php/3473341/SSI-The-Include-Command.htm

It looks like this in modest:

main.xhtml

<?xml version='1.0' encoding='UTF-8'?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <include>reusablePiece</include>
  </head>
  <body>
    <reusablePiece/>
  </body>
</html>

reusablePiece.xml

<header>...Some non-trivial content...</header>

Very simple would be the jQuery .clone() function.

If you have more complex content I recommend looking at Handlebars.js which is a full fledged JS templating engine.

发布评论

评论列表(0)

  1. 暂无评论