简体   繁体   English

如何模拟本地站点的SSI

[英]How to mimic SSI for a local site

I have a block of code for a navbar on a local only site without a web server that I want to have on many pages. 我在一个本地站点上有一个导航栏代码,而没有要在很多页面上使用的Web服务器。 I would have like to have a separate file that has the HTML for the navbar that all the pages reference. 我想有一个单独的文件,其中包含所有页面引用的导航栏的HTML。 That way whenever an update to the navbar needs to be made, it only has to be done once. 这样,每当需要对导航栏进行更新时,只需进行一次即可。

I'm using this Bootstrap template: https://bootstrapmade.com/demo/Regna/ 我正在使用此Bootstrap模板: https : //bootstrapmade.com/demo/Regna/

Years ago, using a different type of navbar system, I was able to achieve this with Javascript. 多年前,使用其他类型的导航栏系统,我能够使用Javascript实现此目的。 I just referenced the file where I wanted the navbar like this: 我只是在想要导航栏的地方引用了文件,如下所示:

<script type="text/javascript" src="js/menu3.js"></script>

And menu3.js was something like this: menu3.js是这样的:

document.write("<div style='float: left' id='my_menu' class='sdmenu'>");

document.write("<div id='section1' class='collapsed'>");
document.write("<span class='menuheader'>SECTION 1</span>");
document.write("<a href='page1.html#anchor1'>Option 1</a>");
document.write("<a href='page1.html#anchor2'>Option 2</a>");
document.write("<a href='page1.html#anchor3'>Option 3</a>");
document.write("</div>");

document.write("<div id='section1' class='collapsed'>");
document.write("<span class='menuheader'>SECTION 2</span>");
document.write("<a href='page2.html#anchor1'>Option 1</a>");
document.write("<a href='page2.html#anchor2'>Option 2</a>");
document.write("<a href='page2.html#anchor3'>Option 3</a>");
document.write("</div>");

That actually worked nicely. 实际上效果很好。 Now, I'm working with a different navbar setup, and that technique doesn't work. 现在,我正在使用其他导航栏设置,但该技术无效。 Nothing displays on the page. 页面上没有任何显示。 Since it's a local site, I can't use PHP or SSI or anything else that I'm familiar with. 由于它是本地站点,因此无法使用PHP或SSI或其他我所熟悉的东西。

What can I do to pull in code from an external file on a local site? 如何从本地站点上的外部文件中提取代码?

using external resources on your local site will trigger a same origin policy warning and can also be prohibited by the external page if CORS is not setup correctly. 如果未正确设置CORS,则在本地站点上使用外部资源将触发相同的原始策略警告 ,并且也可能被外部页面禁止。

Since its a local page for yourself, I would suggest just using an iframe and load your external hosted header.html as it gives you what you want the fastest. 由于它是您自己的本地页面,因此建议您仅使用iframe并加载外部托管的header.html因为它可以为您提供最快的速度。

Otherwise I'd suggest you setup a local webserver with an npmjs module like local-webserver and configure CORS and the same origin policy as outlined in the above linked documentations 否则,我建议您使用npmjs模块(例如local-webserver)设置本地网络服务器,并配置CORS和上述链接文档中概述的原始策略

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM