2

I have this Javscript fade effect when opening the page, for example admin.php.

<body>
<script>
    document.body.className = 'fade';
</script>

...html code...

<script>
  document.addEventListener("DOMContentLoaded", function(e) {
    document.body.className = '';
  });
</script>
</body>

The problem is I can't figure it out how to run this script only when comming from login.php page.

For clarification, it's an effect when opening page it's white and then in about 1sec the page becomes visible, creating fade effect.

2
  • I think what you need to look into is window.history Commented Jun 26, 2019 at 18:20
  • If you're using php then you can set a session flag on login.php and then check on admin.php if that flag is set, and if it does, unset it and output that specific JS to the document. You can also check $_SERVER['HTTP_REFERER'] Commented Jun 26, 2019 at 18:22

2 Answers 2

1

Pass along a variable in the query string. For example, link to it with admin.php?coming_from_login=1.

Then in your PHP file:

<?php if (isset($_GET['coming_from_login']) && $_GET['coming_from_login'] == '1'): ?>
<script>
  document.addEventListener("DOMContentLoaded", function(e) {
    document.body.className = '';
  });
</script>
<?php endif; ?>
Sign up to request clarification or add additional context in comments.

Comments

1

You can make use of document.referrer:

<body>
    <script>
        const referrerPage = document.referrer.replace(location.origin, ""); // Remove the origin to leave just the page
        if (referrerPage === "/login.php") {
            document.body.className = 'fade';
            document.addEventListener("DOMContentLoaded", function (e) {
                document.body.className = '';
            });
        }
    </script>

    ...html code...
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.