简体   繁体   English

我如何使用 w3.js 在 head 标签中包含片段

[英]how do i use w3.js to include snippets in the head tag

With w3.js file it is possible to include html snippets into another html file similar to what we see in php but the downside is that this only works in the body of the page.使用 w3.js 文件,可以将 html 片段包含到另一个 html 文件中,类似于我们在 php 中看到的内容,但缺点是这仅适用于正文。 eg例如

<body> 
<div w3-include-html="./src/nav.html"></div>
<script>w3.includeHTML();</script>
.
.
.
</body>  

My question now: Is there anyway to make this work in the head tag?我现在的问题是:有没有办法在 head 标签中进行这项工作? So that one doesn't have to repeat the same block of multiple links on every single page?这样就不必在每个页面上重复相同的多个链接块?

W3.js can include elements in the head as well as the body. W3.js 可以在头部和正文中包含元素。

I created a quick test to be sure, and it works using:我创建了一个快速测试以确保它可以使用:

<html>
    <head>
        <title>Include Test</title>
        <link w3-include-html="head_include.html"/>
    </head>
    <body>
        <h1>This is a test</h1>
        <div w3-include-html="body_include.html"></div>
    </body>
    <script src="w3.js"></script>
    <script>w3.includeHTML();</script>
</html>

Here it is using the W3.js include HTML functionality to add a stylesheet in to the link tag.这里它使用 W3.js 包含 HTML 功能来将样式表添加到链接标记中。

However:然而:

  • This won't work for "file://" if you are working on this locally, because it is using an AJAX call, and the CORS policy may block it.如果您在本地处理此文件,这将不适用于“file://”,因为它正在使用 AJAX 调用,并且 CORS 策略可能会阻止它。
  • This is relying upon a Javascript executing on the page, altering the DOM, so this won't work if Javscript is disabled on the client, as it will be on things like web crawlers.这依赖于在页面上执行的 Javascript 来更改 DOM,因此如果在客户端上禁用了 Javscript,这将不起作用,因为它将在诸如 web 爬虫之类的东西上运行。
  • Unlike the php includes this is visible to the client.与 php 不同,这对客户端可见。

There are lots of templating alternatives, if that is your goal, and you may wish to look into Server-Side Includes if PHP is not available on your hosting solution, as this will allow you to perform inclusion of HTML with a properly configured server.如果这是您的目标,有很多模板替代方案,如果您的托管解决方案上没有 PHP,您可能希望查看服务器端包含,因为这将允许您使用正确配置的服务器执行包含 HTML。

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

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