Skip to content

Instantly share code, notes, and snippets.

@Albert-W
Last active February 8, 2020 13:32
Show Gist options
  • Save Albert-W/e100e95d4089fb2976adf26a6b50d75f to your computer and use it in GitHub Desktop.
Save Albert-W/e100e95d4089fb2976adf26a6b50d75f to your computer and use it in GitHub Desktop.
将gist嵌入文章中的四种方法

gist提供了代码片段服务,可供插入网页,方面统一入口管理和版本溯源。插入方法如下:

一、通过gist.js

复制embed生成的连接:

<script src="https://gist.github.com/ouqiang/2f22a86e32d1bb53832f.js"></script>

插入到网页中。

缺点:js使用了document.write 无法在异步加载的网页中使用。
优点:格式排版无需操心。

二、通过iframe 与 gist.js

<iframe 
    width="100%"
    height="300"  
    scrolling="auto"  
    src="data:text/html;charset=utf-8,
    <head><base target='_blank' /></head>
    <body><script src='https://gist.github.com/Albert-W/e37d1c4fa30c430c37d7b1b1fe9b60d8.js'></script>
    </body>">

缺点:写法复杂
有点:可在异步加载环境中使用。可以文档内部定义更多功能和属性。

三、通过 object 与 gist.pibb 把js后缀换成pidd后缀, 比如

<object id="ob" width="100%" height="2000" data="https://gist.github.com/Albert-W/5aff60a92e6994de808f08ac53ad47db.pibb"  ></object>

效果同二

四、通过 embed 与 gist.pibb

<embed id="em" width="100%" height="2000" src="https://gist.github.com/Albert-W/5aff60a92e6994de808f08ac53ad47db.pibb" >

效果同二、三

总结:使用document.write函数解决了排版问题;使用github提供的网页,无法解决跨域问题,需要自己调整嵌入体高度。
各有利弊,自行取舍。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment