【问题标题】:Replace page content before file loads?在文件加载之前替换页面内容?
【发布时间】:2011-01-07 14:46:32
【问题描述】:

我想知道,有没有什么脚本可以使用,以便我可以简单地更改一点点,然后内容会更改为多个页面?也许是 PHP 或 htaccess 文件。

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

谢谢!

【问题讨论】:

    标签: php javascript html .htaccess iframe


    【解决方案1】:

    如果您在 htaccess 文件中使用以下 sn-p,您的 url 不会更改但会重定向:

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

    【讨论】:

    • 我认为他有一个 Apache 服务器,因为他说他尝试过一次 htaccess
    【解决方案2】:

    当然有,让我们以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 之外的任何其他数字。

    对不起,我刚刚看了这部分

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

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

    【讨论】:

      【解决方案3】:

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

      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>
      

      【讨论】:

      • 我之前试过。这就是我目前正在使用的。该方法非常有效,因为直到页面加载完成后才调用该函数。有没有办法在不先加载原始页面的情况下做到这一点?
      猜你喜欢
      • 2014-07-21
      • 1970-01-01
      • 2014-08-05
      • 2012-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-15
      • 1970-01-01
      相关资源
      最近更新 更多