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

时间:2010-01-21 06:20:13

标签: php javascript html .htaccess iframe

我想知道,是否有任何我可以使用的脚本,以便我可以简单地改变一个小东西,然后内容将改变多个页面?也许是PHP或htaccess文件。

我想要做的是为我的网站制作一个“正在建设中”页面,我可以定期打开或关闭。现在,我使用的方法是JS,这不太有效。我所做的是它将用正在构建的页面替换body标签的内容,然后将标题更改为“Under Construction”。这个问题,在页面加载后加载函数。所以我要么需要一个JS脚本,它将在页面上的任何内容之前加载,或者需要一个PHP脚本来做同样的事情。我也认为(如果可能的话)htaccess文件也会非常好,因为我可以将它应用到某些目录。我知道我可以使用htaccess文件来重定向用户(我也可以用PHP和JS做到这一点)但我不想这样做,因为我希望url保持不变。如果我将用户从page1.html重定向到underconstruction.html,则会更改浏览器中的网址。我希望网址保留为page1的page1.html,page2的page2.html和page3的page3.html ...有谁知道如何完成这样的任务?如果是这样,请帮助。

谢谢!

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

肯定的是,让我们以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,它将直接转移/重定向到构造页面..完成构建页面后,要么完全删除这部分代码,要么将重定向设置为1旁边的任何其他数字。< / p>

对不起,我刚读了这部分

  

如果我要重定向用户   page1.html到underconstruction.html,   然后改变了网址   浏览器。我希望网址保持原样   page1.html for page1,page2.html for   page2和page3.html for page3 ...   有谁知道我能做到的   这样的任务?

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

答案 2 :(得分:0)

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

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>