从txt文件中自动填充代码到每个html页面? 通过单一代码从txt导入代码到所有html页面。

问题描述 投票:0回答:1

我不想再为每个html文件写同样的代码。有没有办法让下面的代码可以在所有的html页面上使用。

    <script async src="https://www.googletagmanager.com/gtag/js?id="></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }
        gtag('js', new Date());
        gtag('config', '');
    </script>
<meta charset="UTF-8">
    <link rel="icon" href="icon.png">
etc...

我的意思是,例如,在每个html页面上复制head.txt的代码。这将允许我不在所有的页面上写同样的代码片段。

html
1个回答
0
投票

你的问题非常难以理解,但我将尝试。

文件连接使用

<link rel = "import">

:

<head>
   <link rel = "import" href = "/ path / to / imports / stuff.html">
</head>

指定的URL是导入位置。要使用从其他域导入,其位置必须允许跨域资源共享(CORS)。

<! - Resources on other origins must be CORS-enabled. ->
<link rel = "import" href = "http://example.com/elements.html">

注意:浏览器会忽略对同一URL的重复请求。这意味着无论页面上有多少个连接,从一个地址只能进行一次下载。

© www.soinside.com 2019 - 2024. All rights reserved.