3

I have page which created dynamically.

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

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

Is there any way I can detect if js is on with php?

Or is there any way I can remove/hide it by jquery?

Thanks in advance.

4 Answers 4

9

PHP is executed before any browser action takes place, so no, PHP cannot directly detect whether the user has Javascript on or off.

You must provide a bit more info on what you're doing for us to find you a workaround. In PHP, it is possible to detect whether the call was made via AJAX or not using the $_SERVER['X_HTTP_REQUESTED_WITH'] global variable which jQuery will set automatically.

If you need to show an element when Javascript is enabled, you can first hide the element with CSS and enable it with Javascript/jQuery. The same goes the other way around also.

Sign up to request clarification or add additional context in comments.

1 Comment

Furthermore, you can't do anything in jQuery, as it IS javascript, and therefore will not be executed if Javascript is not on.
4

You can't do that in PHP because the page is rendered by the time you know. And apart from some crazy redirect scenario, your best bet may be to use CSS + JS to show/hide what you need:

What I normally do (and your mileage may vary depending on what you need to show/hide) is this:

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

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

      ... everything else
   </head>

Then you can use CSS to hide/show based on if JavaScript is enabled/disabled:

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

2 Comments

@Chacha102 it is not hacky in the least. It provides one of the best ways to progressively enhance your site, while avoiding a Flash of Unstyled Content (FOUS), or worse, the display of content that only is needed if JS is present.
It is hacky! It's just hacky in the good, more historic sense implying a slick and intelligent trick, rather than the more recent pejoritive sense, implying a kludge or otherwise ill-conceived notion.
2

It can't do it directly, and workarounds for it are usually awkward and wasteful.

Use progressive enhancement technique instead.

3 Comments

Ok, what do you recommend to do?
Only put Javascript features in that enhance the functionality of the site. The site should be completely functional without Javascript.
This is also known as Unobtrusive Javascript/Programming. You can read more about it here: w3.org/wiki/The_principles_of_unobtrusive_JavaScript Progressive Enhancement is a prerequsite of Unobtrusive Javascript.
0

Just make the website working without JS. If everything is fine, you can attach JS functionality with e.g. jQuery.

This is also called unobtrusive JavaScript.

So you basically don't distinguish between client with JS and client without JS. You don't provide different output. You set up your HTML code in such a way that you can easily identify the elements that should JS functionality an this functionality in a programmatic way.

This way is even easier for you as you don't have to think about different outputs and it guarantees that the site is also working without JS.

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.