We help IT Professionals succeed at work.

Is Javascript enabled?

dory550
dory550 asked
on
Medium Priority
127 Views
Last Modified: 2019-10-24
Hi guys
I use classic ASP
Is there a way to find out  if user's  JavaScript is enabled or disabled and store the answer in a Boolean variable (True or false)
Thanks
Dory
Comment
Watch Question

Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
If you just want to detect it on the browser, you can use
 <noscript>
         Your browser does not support JavaScript!
      </noscript>

Open in new window

If you want to send data to your database if javascript is detected or not, you would have to detect if it is enabled. To do that, first update the database with a value on page load. Perhaps use the session id.  Next send an ajax request https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started that will send data with the session id and javascriptFlag set to a value of 1 to a field called javascriptFlag. On your ajax processing page, look up the session id and update the javascriptFlag to 1.

That will be a good test. Instead of recording the session id, maybe use a concatenation of the either the date&session id  or ip & session id.  Session id's can get reset and you may find duplicates which can create a false positive/negative.
Chinmay PatelChief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Just... Thinking out loud... Will Ajax work if Javascript is not enabled? 😈
Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
To elaborate, on page load send an xmlhttpost https://support.microsoft.com/en-us/help/290591/how-to-submit-form-data-by-using-xmlhttp-or-serverxmlhttp-object
<%

user_id = date&Session.SessionID              ' CREATE A USERID
Response.Cookies("user_id")=user_id       ' SAVE THE USERID TO A COOKIE

' *********** SEND THE USERID TO THE DATABASE **********************
DataToSend = "user="&user_id
dim xmlhttp 
set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST","https://mysite.com/update_user_info.asp",false
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
xmlhttp.send DataToSend
Response.ContentType = "text"
data=xmlhttp.response.text
        Set xmlhttp = nothing

'******************
You can first check to see if the user_id has been in the database, If it has, then you can check the flag for a 1 or 0 and report back. If it is not available, then create the entry in the database.
'*******************
%>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>TEST</title>
</head>
<body>

<script>
$(function(){
    $.ajax({
  method: "POST",
  url: "/update_user_info.asp",
  data: { name: "<%=user_id %>", javascriptFlag: "1" }
})
});
</script>
<noscript>No javascript detected</noscript>
</body>
</html>

Open in new window

Probably put the first part as an include file to all pages and adjust what I started here to
1) Test if the user_id is in the database
2) If not add it
3) If it is there, send back a 1 or 0 to denote javascript turned on for the session.
4) In the middle of the page, <%%> if data is returned for the javascript flag, run the page, if not redirect or add a message that js is required.
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
detect if javascript enabled

Just redirect the user to another page when "noscript" mean javascript not enabled :

<noscript>
<meta http-equiv="refresh" content="1; URL=RedirectPageHere.html">
</noscript>

Open in new window

Author

Commented:
Hi guys
Please read the question again!
Is there a way to find out  if user's  JavaScript is enabled or disabled and store the answer in a Boolean variable (True or false)?

none of the answers/scripts  above ends with a Boolean variable  that contains True or False
Can anyone please include with his solution a js function that produces a  Boolean variable  that contains True or False

rough example:
if js enabled then yn ="True" else yn ="False"
Thanks
Dory
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
in a Boolean variable (True or false)?

so no javascript but we use javascript to send a boolean right?
as you can see, you can't use javascript if it's not enabled
your solution is to redirect and store this to a boolean and/or session variable and/or cookie :

by default you session/cookie is set to "no/false", if javascript is not enabled just redirect to an asp page and store the information :
<noscript>
<meta http-equiv="refresh" content="1; URL=RedirectPageHere.asp?nojavascript=yes">
</noscript>

Open in new window


also you CAN'T store something on your server BEFORE the client event talk to your server (opened the page) so asking to get information before is a non sense too

after at least one conversation between browser and server, we should be able to get information (boolean to set or anything else)
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
RedirectPageHere.asp :

nojavascript = Request.QueryString("nojavascript"); // you boolean
Session.nojavascript = nojavascript; // set the info in a session

Open in new window

Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
Is there a way to find out  if user's  JavaScript is enabled or disabled and store the answer in a Boolean variable (True or false)?

Yes, however, you didn't specify if you want it client side or serverside. My solution does give that to you.  On the server, you are assuming the flag is 0. If the javascript loads, then it makes a call to the server where you set a flag to 1.

If you are not concerned with getting a response back to the server, then you just need to modify your code to assume js is enabled. If it is not, then it will not be run and thus nothing to to test or set a binary variable to.  If you only want to run the page if js is enabled, then generate the body of the page entirely using javascript. Then add the noscript tag to let the user know they have to have javascript enabled.

Can you explain exactly what you really want to do and perhaps we can give you a better solution?
Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
To illustrate
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>test</title>
  <script>
     window.onload = function() {
    var javascriptFlag =1;
    alert(javascriptFlag);

  };
  </script>  
</head>
<body>
  <noscript>You do not have javscript turned on</noscript>
</body>
</html>

Open in new window

Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
Here is a similar example
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>test</title>
  <style>
    .red{
      color:red;
    }
  </style>
  <script>
     window.onload = function() {
    var javascriptFlag  =1;   
    var content = document.getElementById('results');
    content.insertAdjacentHTML('afterend', '<div class="red">'+javascriptFlag +'</div>');

  };
  </script>  
</head>
<body>
  <div id="results"></div>
  <noscript>You do not have javscript turned on</noscript>
</body>
</html>

Open in new window

If JS is not enabled, then there is no variable to start with. That may be the confusion. This is why I suggested to send the data to the server and you can then use asp to generate pages based on JS being enabled. The way you get the data back about JS being enabled is through an ajax request to your processing page. Of course if JS is not enabled, then there will be no call.

In short, if you only use JS, then there can not be a binary variable because the variable can only be there and present you with a 1 for on or it is non existent. If you use my idea of sending data to the server, then on the server you set a binary field in the database or you could also set a session variable too.  Set the variable to 0. Then if there is an ajax call received, you can set the variable in the db or session to 1. This will take some thought to implement the way you expect.
CERTIFIED EXPERT

Commented:
Hi,

There is no reliable way to check using server-side for JavaScript detection.
But it is possible to set a cookie using JavaScript, and then check for that cookie using server-side script on next page views like it was proposed by other experts...

I'm using Javascript and Moderniz https://modernizr.com/
I'm curious to know why you can't use Javascript for that?
Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
@lenamtl, I just outlined an option to do this server side, I am confused why you say it is not possible? Please review my code again.  Solely relying on cookies is not a good option because you can have the browser set to not accept cookies yet have javascript enabled. That would give a false negative.  Modernizer only works if JS is enabled.

The goal set out by this question is, "  JavaScript is enabled or disabled and store the answer in a Boolean variable". This is not really possible as stated unless you first create a variable that is set to false server side by either a session variable or storing back to the server. Then send an ajax request back to the server. If the ajax request comes back, then set the flag to true.  This would be similar to a log in system INPUT->SERVER->UPDATE CLIENT->Allow or Deny.
CERTIFIED EXPERT

Commented:
@scott
I'm just saying that is not reliable to use server side code to detect Javascript.
Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
You have to think opposite. On the server, you default to not enabled until you get a call back. If you want to discuss in more detail, please send me a PM to not take up the thread here.

Author

Commented:
Scott Fell
and other contributors
Sorry I took so long to respond
Please note my  website does not prevent an unregistered new user from placing an order. the problem is he would not be able to
place an order if JS is not enabled nor would he be able to register
I do not want prevent new users from placing an order,  I don't want to force a guest to register  before he can browse to his heart desire
95% percent or more of new users are JS enabled anyway
My website is using frames (I know it's not too smart, but it will changed soon)
Every webpage on my website has the folowing js script:

<SCRIPT >
<!--
if (top == self) self.location.href = "../default.asp"
-->
</SCRIPT>

I am a wholesaler for Health & beauty aids and c-stores items (non-food).  
I carry about 1500 items and prefer not reduce the size my webpages which are around 60- 80kb each
I need to prevent some bad guys from crushing my website....
If JS is disabled they can target single webpages remotely repeatedly and slow my website or crush it
If I know a user has JS disabled I could add a <noscript> message above and terminate his session (session.end)
Any sugestions?
Dory
Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
If JS is disabled they can target single webpages remotely repeatedly and slow my website or crush it

javascript or not, it's doable

<noscript>
<meta http-equiv="refresh" content="1; URL=killsessionbecauseJSdisabled.asp?nojavascript=yes">
</noscript>

Open in new window

Scott FellDeveloper & EE Moderator
CERTIFIED EXPERT
Fellow
Most Valuable Expert 2013

Commented:
If that is what you are worried about, I think you are better off doing something on the server or use your asp code.  If somebody is going to try and hit you with a DoS attack, not having js enabled or not is not going to help you. Check with your webhost on some ideas. My host installs their own software for this type of thing.  On your own, you can track visits to your site by IP or just session.id in asp. If that session.id or IP goes beyond a certain point in a specific amount of time, send to another site.  Make sure to white list good bots like google. This is why a third party program may be the better option.

Author

Commented:
Leakim971
Thank you for your help
Dory

Scott Fell
Thank you for your help
Dory
leakim971Multitechnician
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
you welcome

Author

Commented:
lEAKIM971
Your solution was very helpful to me ......thanks
You made a minor mistake  which I corrected (you did not enclose the url with single qoutes)
<noscript>
<meta http-equiv="refresh" content="1; URL='killsessionbecauseJSdisabled.asp?nojavascript=yes'">
</noscript>

Explore More ContentExplore courses, solutions, and other research materials related to this topic.