tags:

views:

225

answers:

6

hi friends,

I want to get the javascript status of any browser. How do i get this.?

Means when page load it should display that javascript in that browser is enable or disable.

I m using php.

Thanks In advance......

A: 

In the body tag call a javascript function onload. That function sets a javascript variable that JS is enabled in this browser. By default you assume that JS is disabled.

The Javascript will get called only if JS is enabled otherwise nothing will happen. In that function you can use document.write to display what you want.

Bhushan
+7  A: 

Add your message for javascript not enabled browsers in tag.

Example :

<noscript>
<p> Javascript is not enabled. Please enable it. <p>
</noscript>

So if javascript is not enabled then the above message will be displayed.

Shoban
+2  A: 

If you want to detect on server side if the browser runs js - run some test script in the browser, which sends ajax post request with the result back to the server

alternatively have javascript set a cookie

then compare result with expected.

Evgeny
If you need to know on the server-side then setting a cookie is probably your safest bet.
Prestaul
A: 

The most original way that I can think of is to have the HTML output a JavaScript function call, for example postbackDoesExecute(), which makes an AJAX request to a PHP script, call it jsCallback.php. When jsCallback.php is called from the AJAX request, it updates the user's session (or a database entry, depending on how you choose to implement it) with Javascript = true or something. If this entry is not recorded, then it is evident that either the request has timed out or the JavaScript call to postbackDoesExecute() never happened, meaning that JavaScript is not enabled.

Just a thought...

Jason
+1  A: 

Might be slight overkill but this script tries to run the function jsTest. If run, it will set the text of the div to JavaScript enabled. If it does not run the text stating the JavaScript is disabled will remain.

<html>
<head>
<script type="text/javascript">
function jsTest()
{
  var jsStatusDiv = document.getElementById("jsStatus");
  jsStatusDiv.innerHTML = "JavaScript Enabled";
}
</script>
</head>
<body onload="jsTest();">
<div id="jsStatus">JavaScript Disabled</div>
</body>
</html>
Jesse
A: 

If you need your application to display two different states depending on JavaScript availability, you could try something hacky like creating a midway page.

This page would contain something like

<meta http-equiv="refresh" content="5;URL=mysite.php?js=0">
<script type="text/javascript">
  window.location.replace("mysite.php?js=1");
</script>

This will redirect people with Javascript to one page and the others to a different page. It is a bit hacky though.

If your users go through a login page you can also have an extra hidden field in your login page:

<input id="hasJs" type="hidden" name="hasJs" value="">

And then

<script type="text/javascript">
  document.getElementById('hasJs').value = '1';
</script>

Hth Sorin

Sorin Mocanu