Solved

trying to detect js via form

Posted on 2011-03-01
20
830 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
 
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
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 …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now