Solved

trying to detect js via form

Posted on 2011-03-01
20
835 Views
Last Modified: 2013-12-24
on the top of a page I have a hidden form to detect js but it doesn't seem to be giving the value yes when js is on.

any ideas?
<script>
	function runDetectection() {
		document.getElementById('isJsEnabled').value = "yes"; 
	}
</script>
<body onLoad="runDetectection();">
<cfparam name="isJsEnabled" default="no">
<cfparam name="form.isJsEnabled" default="no">

<form name="testjs" action="boo" method="post">
<input type="Hidden" name="isJsEnabled" id="isJsEnabled" value"no"/>
</FORM>

Open in new window

0
Comment
Question by:Shawn
  • 11
  • 8
20 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 35011890
The overall logic is fine. How is the form being submitted? I'm assuming the hidden field is part of a bigger form that gets submitted .. ?
0
 
LVL 1

Author Comment

by:Shawn
ID: 35011932
the thing is the actual form is not being submitted. The user just clicks on a link to log in. If he/she has opted to have the password remembered the login form is bypassed.

I may not need the little form. I just need to set js as being enabled or not.
0
 
LVL 2

Expert Comment

by:dlewis9
ID: 35012107
Your code will set the value of the hidden form variable, but it can't be used in ColdFusion from the same page (the CF stuff has already executed server side, and JS is client side)..

You could do this script on a page with a submit button, and then on the action page read the form value and decide if the user can continue or needs to be redirected to tell them to have JS turned on.

Here's someone else who did that:
http://www.webmasterkb.com/Uwe/Forum.aspx/coldfusion-advanced/5189/Detect-javascript-enabled
0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 
LVL 52

Expert Comment

by:_agx_
ID: 35012113
The form  (or something similar) has to be submitted to the server ... or you won't be able to check the value.
0
 
LVL 1

Author Comment

by:Shawn
ID: 35012163
what about in the link rather than a form?? is that possible?

<a href="#script_name#?action=affiliate.afflogin">Login</a>
0
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 35012171
You could do the same thing with a link. Just use javascript to add a parameter to it on page load

<!--- test.cfm --->
<script type="text/javascript">
      function changeLink() {
            var elem = document.getElementById('loginLink');
            elem.href = elem.href +"?isJsEnabled=true";
      }
</script>

<body onLoad="changeLink()">
      <a href="login.cfm" id="loginLink">Login</a>
</body>

<!--- login.cfm --->
<cfparam name="url.isJsEnabled" default="false">
<cfdump var="#url#">
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35012202
Our posts clashed.

If your link already has parameters then use "&" to append another one instead of "?"

      function changeLink() {
            var elem = document.getElementById('loginLink');
            var sep  = (elem.href.indexOf("?") >= 0 ? "&" : "?");
            elem.href = elem.href + sep +"isJsEnabled=true";
      }
0
 
LVL 1

Author Comment

by:Shawn
ID: 35012241
i think I'm missing something. ere's what I have:

<script type="text/javascript">
      function changeLink() {
            var elem = document.getElementById('loginLink');
            var sep  = (elem.href.indexOf("?") >= 0 ? "&" : "?");
            elem.href = elem.href + sep +"isJsEnabled=true";
      }
</script>

<cfoutput>
<body onLoad="changeLink()">  
        <p><a href="#script_name#?action=affiliate.afflogin" id="loginLink">Login</a></p>
</body>  
</cfoutput>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35012305
What you have works fine for me on a test page.  What's it NOT doing for you?

Results when javascript is disabled:
struct
ISJSENABLED       false
action       affiliate.afflogin

... results when javascript IS enabled:
struct
action       affiliate.afflogin
isJsEnabled       true
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35012319
Btw: My test action page is just:

    <cfparam name="url.isJsEnabled" default="false">
     <cfdump var="#url#">
0
 
LVL 1

Author Comment

by:Shawn
ID: 35012348
i am gettin
ISJSENABLED       false
action       affiliate.afflogin

whether it is on or not.

When I hover over the link I do not see the added part in the url. shouldn't I?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35012365
Yeah, you should.  Are you running the exact code posted? Because it works fine in both FF and IE8.  Which suggests you're doing something different ;-)
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35012373
Also make sure you refresh the page after re-enabling/disabling javascript.
0
 
LVL 1

Author Comment

by:Shawn
ID: 35012376
this is in a template inside another page. I'm pretty sure there is already a body element. would that make a difference?
0
 
LVL 1

Author Comment

by:Shawn
ID: 35012407
my exact code...

dsp page:
<script type="text/javascript">
      function changeLink() {
            var elem = document.getElementById('loginLink');
            var sep  = (elem.href.indexOf("?") >= 0 ? "&" : "?");
            elem.href = elem.href + sep +"isJsEnabled=true";
      }
</script>

<cfoutput>
<body onLoad="changeLink()">  
        <p><a href="#script_name#?action=affiliate.afflogin" id="loginLink">Login</a></p>
</body>  
</cfoutput>

action page:
<cfparam name="url.isJsEnabled" default="false">
<cfdump var="#url#">
<cfabort>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35012433
Nope. Works perfectly.  

Are you using url re-writing or routing pages through some handler or framework?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35012450
this is in a template inside another page. I'm pretty sure there is already a body element. would that make a difference?

Yes ... I imagine only one body tag fires: the first one.  To test it, add an alert to your function and you'll probably see it's not firing
    function changeLink() {
         alert('called changeLink');
         ...
     }
0
 
LVL 1

Author Comment

by:Shawn
ID: 35012461
yep, not firing
0
 
LVL 1

Author Comment

by:Shawn
ID: 35012489
:-D that did it. Found the first body and inserted it. works perfectly now.

thank you!
0
 
LVL 52

Expert Comment

by:_agx_
ID: 35012493
Great!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Coldfusion RegEx 8 75
Stream live video from Raspberry Pi camera 22 250
Fixing Old Legacy Code 1 45
Coldfusion cfscript DELETE function 7 36
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question