0

I currently have the code below (PHP)

<?php
    require_once __DIR__.'/includes/Connect.php';
    require_once __DIR__.'/includes/Nojs.php';
    require_once __DIR__.'/includes/Main.php';
    require_once __DIR__.'/includes/footer.php';
?>

I want Main.php to load only if JavaScript is enabled, and Nojs.php to load only is JavaScript is disabled.

Surrounding Nojs.php with tags does nothing (See below code)

<?php
    require_once __DIR__.'/includes/Connect.php';
?>
<noscript>
    <?php
        require_once __DIR__.'/includes/Nojs.php';
    ?>
</noscript>
<?php
    require_once __DIR__.'/includes/Main.php';
    require_once __DIR__.'/includes/footer.php';
?>

Since I know the PHP cannot access the browser as it is done server-side, is it possible to try to use HTML (Like I attempted) or other JavaScript to get it to work? Thanks in advance.

EDIT:

Creating a new file for nojs.php and using meta refresh works, but there is a noticeable delay. The page starts loading the original URL and then redirects to the other. A visitor can quite easily cancel the page load process and stop the page load before the redirect. They will have a partly loaded page, but all the content they really need will be there. Is there a way to force an immediate redirect (Yes, "content" is set to "0" per the below anwser)?

Greenreader9
  • 491
  • 4
  • 12
  • Does this address your situation: https://stackoverflow.com/questions/12361321/calling-javascript-based-on-php-conditional-when-libraries-load-in-the-footer – Chris Strickland Nov 20 '21 at 00:04
  • As you say "PHP cannot access the browser as it is done server-side" — the ` – Stephen P Nov 20 '21 at 00:12
  • Does this answer your question? [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – gre_gor Nov 20 '21 at 01:05

1 Answers1

2

Create another PHP file with no js version when first PHP loaded it will check if no js support will redirect to no js version of PHP


<noscript>
  <meta http-equiv="refresh" content="0;url=nojs-version.php">
</noscript>    

Edit: It is also mentioned by W3C as technique without confusing user
https://www.w3.org/TR/WCAG20-TECHS/H76.html

owenizedd
  • 1,187
  • 8
  • 17
  • I thought that using meta-refresh is considered bad practice and should never be used. Am I wrong? – Greenreader9 Nov 20 '21 at 00:36
  • I don’t think so, please elaborate, for what reason? – owenizedd Nov 20 '21 at 00:46
  • 1
    Let’s talk with official data https://www.w3.org/TR/WCAG20-TECHS/H76.html It clearly states `The objective of this technique is to enable redirects on the client side without confusing the user.` – owenizedd Nov 20 '21 at 00:52
  • what do you mean? noscript tag is for checking wether browser support js or not. Im just going to let the data talks here again https://developer.mozilla.org/en-US/docs/Web/HTML/Element/noscript – owenizedd Nov 20 '21 at 01:15
  • Although "content" is set to 0, it takes some time (like 2 seconds) to redirect. In that time, the visitor can just cancel the page load and nothing is affected. – Greenreader9 Nov 20 '21 at 01:22