Solved

trying to detect js via form

Posted on 2011-03-01
20
836 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

732 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