Skip to content

Instantly share code, notes, and snippets.

@Roger-WIN
Last active April 23, 2020 02:40
Show Gist options
  • Select an option

  • Save Roger-WIN/f9ac8d1a4a26754abb82ecc6497a2826 to your computer and use it in GitHub Desktop.

Select an option

Save Roger-WIN/f9ac8d1a4a26754abb82ecc6497a2826 to your computer and use it in GitHub Desktop.
为了静态访问存储在 OSS 上的网页,请设法将该代码片段插入到网页的 HTML 代码中
/* 在页面的 URL 中添加页面的名称,以实现静态访问 */
(() => {
const url = window.location.href, // 当前页面的 URL
pathName = window.location.pathname;
const pageName = "index.html"; // 静态访问页面的名称
let newUrl; // 将要产生的 URL
let flag_append = '/', // 标志 1,表示目录
flag_insert = "/#"; // 标志 2,表示目录下的某级标题
if (url.endsWith(flag_append) && pathName != flag_append) // 以标志 1 结尾,且不是根目录
newUrl = url.concat(pageName); // 直接将页面名称追加到目录后
else if (url.includes(flag_insert)) { // 包含标志 2
var index = url.lastIndexOf(flag_insert) + 1; // 获取标志 2 中第二个字符的位置
newUrl = url.substring(0, index).concat(pageName, url.substring(index)); // 将页面名称插入到标志 2 的字符之间
} else
return;
window.location.replace(newUrl); // 替换为新的 URL
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment