开发者

Menu list across multiple sites using only HTML & CSS

Is there a way to share HTML across multiple pages? For e.g., a common menu bar or footer. I know this can be done using php include, however the webserver that I have space on a开发者_JAVA技巧llows only HTML/CSS.


You could do it using jQuery and AJAX calls, if you really have to do the inclusion client-side. Use something like this:

<script type="text/javascript">
$(document).ready(function() {
    $.ajax({
        url: 'http://www.example.com/header.htm',
        success: function(data) {
            $("#header").html(data);
        },
    });
}
</script>

You then just need to put a div with an id of 'header' (or whatever you replaced #header with in that script above) into the body of your HTML like so:

<div id="header"></div>

That should do the trick. Obviously it's better to do it server side - as anybody with Javascript turned off wouldn't see any files 'included' in this way, but your question covers the reasons behind this!


I think you could use Javascript and AJAX calls to get the common HTML content and dynamically insert it in your page body.

Another solution would be to use iframes.


Save the HTML as a string in an included JavaScript file and use innerHTML() to render it into a element on your pages. You can trigger rendering by an onLoad() attribute in your tag.

JavaScript file: var html= "your HTML code";

HTML page: ...

... ...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜