开发者

Replace page content before file loads?

开发者 https://www.devze.com 2022-12-18 03:33 出处:网络
I wanted to know, is there any sory of scripting that I can use so that I can simply change one littl开发者_StackOverflowe thing and then the content will change for multiple pages? Maybe PHP or the h

I wanted to know, is there any sory of scripting that I can use so that I can simply change one littl开发者_StackOverflowe thing and then the content will change for multiple pages? Maybe PHP or the htaccess file.

What I want to do is make an "Under Construction" page for my website, that I can periodically turn on or off. Right now, the method I am using is JS, which is not too effective. What I have it doing is it will replace the body tag's content with that of the under construction pages, and then change the title to be "Under Construction". The problem with this, the function loads after the page has loaded. So I either need a JS script that will load before anything on the page does, or a php script that does something near the same thing. I also thought (if it was possible) the htaccess file would be really nice too, because I could apply it to certain directories. I know I can use the htaccess file to redirect the user (and I can do that with PHP and JS too) but I do not want to do that because I want the url be stay the same. If I were to redirect the user from page1.html to underconstruction.html, then that changes the url in the browser. I want the url to stay as page1.html for page1, page2.html for page2, and page3.html for page3... Does anyone know how I can accomplish such a task? If so, please help.

Thank you!


If you use the following snippet in an htaccess file, your url does not change but redirects:

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


Sure thing there is lets take ux9i example and re-write it slightly:

// 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>

This ought to do the trick, if I were you I would be using redirection instead of this, here is what I mean:

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

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

Leave the html as it is. Cheers

EDIT

Here is how to do it in php, you'll make page called lets say "construction.html" , now inside of your php page at the top place this code

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

It will directly transfer/redirect you to the construction page in case redirect is set to one .. after you're done with the construction page either remove this portion of code completely or set redirect to any other number beside 1.

Sorry I just read this part

If I were to redirect the user from page1.html to underconstruction.html, then that changes the url in the browser. I want the url to stay as page1.html for page1, page2.html for page2, and page3.html for page3... Does anyone know how I can accomplish such a task?

You should let other people know when you edit your question, you should use then iframe, try to google what is iframe and how to use it. Then you can apply the same logic described above to iframes as well.


Have all your pages include this script.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号