前端模拟支持<!--#include file="/footer.shtml"-->命令

之前的的网页都是html后缀,不支持比如shtml类似命令<!--#include file="/footer.shtml"-->

很多相同的内容不能通过include 进来。之后想将公司的前端页面改成shtml。让很多相同的内容可以通过include导进来。

让shtml支持include这种语法有的还需要服务器配置一下。大多数自己起的服务器对shtml处理当成html

因为前端人员多,有的不想配置服务器,也不想乱搞。所以要开发一个js对<!--#include file="/footer.shtml"-->处理,本地服务器不支持<!--#include file="/footer.shtml"-->语法通过js出来,到了服务器就可以配置一下服务器让支持<!--#include file="/footer.shtml"-->语法,如果服务器处理了,<!--#include file="/footer.shtml"-->标签就没有了。如果没有处理有js出来。

具体代码实现,简单粗暴。需要jq

//让ie低版本支持include标签,include包裹不会破坏结构
document.createElement('include');
var _innerHTML = document.body.innerHTML;
var _html = _innerHTML.replace(/\<!--#include(\s)+(file.+?)--\>/g, '<include class="include" $2></include>');
document.body.innerHTML = _html;
$('.include').each(function() {
        $(this).load($(this).attr('file'))
});

  

posted @ 2017-03-23 16:27  lsdfly  阅读(1016)  评论(0编辑  收藏  举报