4 Star 18 Fork 4

张鑫旭 / HTML import支持

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

自定义HTML imports能力

介绍

借助废弃的HTML imports语法实现HTML或者其他代码片段的include功能。

使用说明

可以使用任意方式即可,可以在页面任意位置,例如直连:

<script src="./html-import.js"></script>

例如import导入:

<script>
    import './html-import.js';
</script>

如果需要兼容Safari浏览器,需要引入 safari-polyfill.js,且 safari-polyfill.js 需要在 html-import.js的前面加载。

<script src="./safari-polyfill.js"></script>
<script src="./html-import.js"></script>

此时,页面中设置了is="ui-import"<link>元素就有了include的能力,例如下面的HTML代码就会呈现header.html的内容效果:

<link rel="import" href="header.html" is="ui-import">

支持load和error两个事件:

link.addEventListener('load', function () {
    console.log('加载成功并结束触发');
});
link.addEventListener('error', function () {
    console.log('加载出错触发');
});

本项目采用内置自定义元素实现,并不支持IE浏览器。

具体兼容性如下表所示:

IE Firefox 63+ Chrome 67+ Safari 10.1+(需polyfill) iOS Safari 10.3+(需polyfill) Android

更多信息可以访问:https://www.zhangxinxu.com/wordpress/?p=10009

许可证

MIT,可以任意修改,保留 JS 代码顶部原作者版权信息即可。

MIT License Copyright (c) 2021 张鑫旭 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

借助废弃的html import语法实现HTML或者其他代码片段的include功能。 展开 收起
JavaScript 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
JavaScript
1
https://gitee.com/zhangxinxu/html-import.git
git@gitee.com:zhangxinxu/html-import.git
zhangxinxu
html-import
HTML import支持
master

搜索帮助