繁体   English   中英

在加载文件之前替换页面内容?

[英]Replace page content before file loads?

我想知道,是否可以使用某种脚本编写方式,以便仅更改一件小事,然后内容就会在多个页面中更改? 也许PHP或htaccess文件。

我想做的是为我的网站创建一个“正在建设中”页面,我可以定期打开或关闭该页面。 现在,我使用的方法是JS,效果不太好。 我正在做的是将body标签的内容替换为在建页面的内容,然后将标题更改为“ Under Construction”。 与此相关的问题是,该函数在页面加载后加载。 所以我要么需要一个在页面上的任何东西之前都可以加载的JS脚本,要么需要一个可以完成相同操作的php脚本。 我还认为(如果可能的话)htaccess文件也将非常不错,因为我可以将其应用于某些目录。 我知道我可以使用htaccess文件重定向用户(我也可以使用PHP和JS进行重定向),但是我不想这样做,因为我希望URL保持不变。 如果我要将用户从page1.html重定向到underconstruction.html,则将更改浏览器中的url。 我希望URL保持为page1的page1.html,page2的page2.html和page3的page3.html ...有人知道我如何完成这样的任务吗? 如果是这样,请提供帮助。

谢谢!

如果您在htaccess文件中使用以下代码段,则您的网址不会更改,而是会重定向:

RewriteEngine on
RewriteRule ^(.*)$ ./under_construction.html [L]

当然,这里有ux9i例子,并稍作重写:

// Set this to true to rewrite the page
var underConstruction = true;

function rewritePage(){
    if (underConstruction){
        document.getElementById ('entirePage').innerHTML =
            "<h1>Under construction</h1>";
    }
}

Test.html

 <html>
    <head>
<script type="text/javascript">
window.onload = rewritePage; 
</script>
        <title>write example</title>
        <script type="text/javascript" src="UnderConstruction.js"></script>
    </head>
    <body>
        <div id="entirePage">
            <p>Some document content.</p>
        </div>
    </body>
    </html>

这应该可以解决问题,如果我是您,我将使用重定向而不是重定向,这是我的意思:

// Set this to true to rewrite the page
var underConstruction = true;

function rewritePage(){
  if (underConstruction){
location.href = 'contruction.html';
  }
}

保留html不变。 干杯

编辑

这是在php中的操作方法,您将创建一个名为“ construction.html”的页面,该代码现在位于您的php页面的顶部

<?php
$redirect = 1;
if($redirect == 1){     
header('Location: construction.html');
}
?>

如果将重定向设置为一个..,它将直接将您转移/重定向到构建页面。在完成构建页面后,要么完全删除这部分代码,要么将重定向设置为除1以外的任何其他数字。

对不起,我刚刚读了这一部分

如果我要将用户从page1.html重定向到underconstruction.html,则将更改浏览器中的url。 我希望URL保持为page1的page1.html,page2的page2.html和page3的page3.html ...有人知道我如何完成这样的任务吗?

当您编辑问题时,您应该让其他人知道,您应该先使用iframe,然后尝试搜索什么是iframe以及如何使用它。 然后,您也可以将上述相同的逻辑应用于iframe。

让您的所有页面都包含此脚本。

UnderConstruction.js:

// Set this to 1 to rewrite the page
var underConstruction = 0;

function rewritePage ()
{
    if (underConstruction)
    {
        document.getElementById ('entirePage').innerHTML =
            "<h1>Under construction</h1>";
    }
}

Test.html:

<html>
<head>
    <title>write example</title>
    <script type="text/javascript" src="UnderConstruction.js"></script>
</head>
<body onload="rewritePage()">
    <div id="entirePage">
        <p>Some document content.</p>
    </div>
</body>
</html>

暂无
暂无

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

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