Can php detect if javascript is enabled or not?

I have a page created dynamically.

Now I want to add an ajax function, so I want to add an if statement to change the outputs.

if(js is on){
 ...
 ... 
 echo "js is on";
}else{
...
echo "js is off";
}

Is there any way to determine if js is enabled with php?

Or is there a way to remove / hide it using jquery?

Thanks in advance.

+3
source share
4 answers

PHP is executed before any browser action occurs, so no, PHP cannot directly determine whether Javascript is turned on or off.

, , . PHP , AJAX $_SERVER['X_HTTP_REQUESTED_WITH'], jQuery .

, Javascript, CSS Javascript/jQuery. .

+9

PHP, , . , - , CSS + JS / , :

( , /):

<html>
   <head>
      ... other stuff here, title, meta, etc ...

      <script type="text/javascript">document.documentElement.className += " js"</script>

      ... everything else
   </head>

CSS, / , JavaScript /:

/* Hide by default, show if JS is enabled */
#needsJS     { display: none  }
.js #needsJS { display: block }

/* Show by default, hide if JS is enabled */
.js #fallback { display: none }
+4

, .

.

+2

JS. , JS-, . JQuery.

JavaScript.

Thus, you basically do not distinguish between a client with JS and a client without JS. You do not provide different output. You will customize your HTML code so that you can easily identify the elements that JS should function with this functionality programmatically.

This method is even easier for you, since you do not need to think about different exits, and this ensures that the site also works without JS.

0
source

Source: https://habr.com/ru/post/1730764/


All Articles