2010-01-21 68 views
0

我想知道,是否有任何脚本可以使用,这样我就可以简单地改变一件小事,然后内容会改变为多个页面?也许PHP或htaccess文件。在文件加载之前替换页面内容?

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

谢谢!

回答

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> 
+0

我以前试过。这是我目前使用的。该方法对于在页面加载完成之前不调用该函数的事实非常有用。有没有办法在不加载原始页面的情况下执行此操作? – 2010-01-21 07:33:03

2

如果您在一个htaccess文件中使用下面的代码片段,您的网址不会改变,但重定向:

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

需要Apache。 – 2010-01-21 12:09:37

+0

我认为他有一个Apache服务器,因为他说他曾尝试htaccess一次 – matte 2010-01-21 21:30:05

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'; 
    } 
} 

保持原样。干杯

编辑

这里是如何做到这一点在PHP中,你会是所谓的网页可以说“construction.html”,现在你的PHP页面内顶部位置的代码

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

这将直接传输/重定向你到施工页面的情况下,重定向被设置为一个..你与施工页面完成后,要么完全去除这部分程序或设置重定向到任何其他的号码1的旁边。

对不起,我刚才读这部分

如果我是用户从 page1.html到underconstruction重定向。html, 然后更改 浏览器中的网址。我希望URL保持为 page1.html对于page1,page2.html对于 page2和page3.html对于page3 ... 有没有人知道我可以如何完成 这样的任务?

你应该让其他人知道,当你编辑你的问题,你应该使用iframe中,然后,试着google一下是iframe和如何使用它。然后,您可以将上述相同的逻辑应用于iframe。