enfield
enfield

Reputation: 841

Inline javascript checks in PHP

I was trying to use <script> <noscript> checks inline with my PHP to render different form elements depending on weather or not someone has javascript enabled.

To display something to non enabled users only I can successfully use

<noscript> <?php ... ?> </noscript>

To display something to enabled users only I tried whats below but it does not work the same as above. Instead it allows all users to still see the PHP.

<script> <?php ... ?> </script>

How can I limit something to only those with javascript enabled without having two totally different pages?

Upvotes: 1

Views: 3109

Answers (3)

Luke Stevenson
Luke Stevenson

Reputation: 10341

Javascript runs on the Client-side (in the browser), PHP runs on the Server-side (before the page is sent to the browser). So PHP does not know whether the browser does, or does not, have Javascript.

The simplest solution is to use Javascript itself to show/hide various elements within the browser, like so:

<!doctype html>
<html>
<head>
<title>Test Page</title>
<style>
.showWithJS {
  display:none;
}
</style>
</head>
<body>

<div class="showWithJS">
  This DIV, and anything with the class "showWithJS",
  will only be seen when Javascript is enabled.
</div>
<div class="hideWithJS">
  This DIV, and anything with the class "hideWithJS",
  will not be seen when Javascript is enabled.
</div>

<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
$(document).ready(function(){

  $('.showWithJS').show();
  $('.hideWithJS').hide();

});
</script>
</body>
</html>

Another option would be to use Javascript to set a cookie (which can be read by PHP) to indicate whether that javascript is running. The downside of that, of course, is that, should the user switch javascript on/off mid-visit, the cookie will not update to reflect that change.

<?php

$javascriptOn = ( isset( $_COOKIE['javascript'] ) && $_COOKIE['javascript']=='on' );

?><!doctype html>
<html>
<head>
<title>Test Page</title>
</head>
<body>

<?php if( $javascriptOn ){ ?>
<div>
  This DIV will only be seen when Javascript is enabled.
</div>
<?php } ?>
<?php if( !$javascriptOn ){ ?>
<div>
  This DIV will not be seen when Javascript is enabled.
</div>
<?php } ?>

<script>
document.cookie = 'javascript=on';
</script>
</body>
</html>

The downside of this is that you need a pageload to set the cookie (it cannot be set and read by the same pageload), so you would need to refresh the page to see the changes.

Upvotes: 1

James L.
James L.

Reputation: 4097

Load the PHP in javascript and then insert when the DOM loads

JS:
function pageLoad(){
  var text = "<?php ... ?>";
  document.getElementById('scriptSpan').innerHTML = text;
}

html:
<span id="scriptSpan"></span>

Upvotes: 0

icktoofay
icktoofay

Reputation: 129001

You may be able to use something like this:

<script>
    <?php
        ob_start();
        // ...
        $scriptOnlyData = ob_get_clean();
    ?>
    document.write(<?php echo json_encode($scriptOnlyData); ?>);
</script>

Upvotes: 1

Related Questions