Solved

trying to detect js via form

Posted on 2011-03-01
20
832 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
LAMP problem identifier tool ? 9 116
Problem to go to page 12 92
Stream live video from Raspberry Pi camera 22 149
How to add a blog at the end of my web pages - using Cold Fusion 3 35
The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

786 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