0

Does anyone know how to do a redirect in coldfusion similar to this redirect in javascript? The issue I am having with javascript is that people are just turning javascript off.. Also is it possible to write something to force them to either keep javascript on or make them download it if they do not have it?

<script language="javascript">
function SelectRedirect(){
// ON selection of section this function will work
//alert( document.getElementById('pcount').value);

switch(document.getElementById('pcount').value)
{
case "0":
alert('Please select number of owners.');
window.location="";
break;

case "1":
window.location="One/ownerInfo1.cfm";
break;

case "2":
window.location="Two/ownerInfo2.cfm";
break;

case "3":
window.location="Three/ownerInfo3.cfm";
break;

  case "4":
  window.location="Four/ownerInfo4.cfm";
break;

case "5":
window.location="Five/ownerInfo5.cfm";
break;

}// end of switch 
}
////////////////// 
</script>
</head>

<h3>How many owners are taking title?&nbsp;
<SELECT id="pcount" NAME="pcount">
<Option value="0">Select Section</option>
<Option value="1">1</option>
<Option value="2">2</option>
<Option value="3">3</option>
<Option value="4">4</option>
<Option value="5">5</option>
</SELECT></h3>


<input type="submit" name="Submit" value="Next" onClick="SelectRedirect();">
  • 3
    FYI, client side [javascript](https://en.wikipedia.org/wiki/JavaScript) is built into most all browsers. It is not something you download. Due to browser security restrictions, you cannot force the client to enable it via server side code. The best you can do is indicate the site requires javascript [via ` – Leigh Sep 30 '14 at 12:55

2 Answers2

3

Also is it possible to write something to force them to either keep javascript on or make them download it if they do not have it?

No, that is not possible, only thing you could do is to block output if JS was not detected and inform your visitors in a <noscript> block, but i would consider that very bad practice. Build your applications in a way that they work with both JS on and off.

That being said, you should start from the JS-less version and add JS functionality unobtrusively. E.g. wrap the <select> and <input type="submit"> in a <form> which action targets a server-side action, only then add JS as kind of a performance-booster, but never rely on JS alone.

Quick code example:

<cfif structKeyExists(url, 'pickPage') and structKeyExists(form, 'pcount')>
    <cfswitch expression=#form.pcount#>
        <cfcase value="0">
            <cfoutput>Error</cfoutput>
        </cfcase>
        <cfcase value="1">
            <cflocation url="One/ownerInfo1.cfm">
        </cfcase>
    </cfswitch>
</cfif>

<form action="?pickPage" method="post">
    <SELECT id="pcount" NAME="pcount">
    <Option value="0">Select Section</option>
    <Option value="1">1</option>
    </SELECT>
    <input type="submit" name="Submit" value="Next" onclick="SelectedRedirect(); return false;">
</form>

<script type="text/javascript">
function SelectRedirect() {
    switch (document.getElementById('pcount').value) {
        case "0":
            alert('Please select number of owners.');
            window.location = "";
            break;

        case "1":
            window.location = "One/ownerInfo1.cfm";
            break;

    }
}
</script>
wiesion
  • 2,349
  • 12
  • 21
  • As far as I can see you're not setting a pickPage parameter, your select will add "pcount=1" etc. to the url. You should also use method="post" for your form submit. – Andreas Schuldhaus Sep 30 '14 at 13:46
  • @AndreasSchuldhaus thanks you're right, just wanted to use the `?pickPage` parameter in the form to inform CF that this form was submitted. Also, this was just a quick example how to do it, so i didn't really care about GET or POST. edited my response – wiesion Sep 30 '14 at 13:57
  • nothing, it's just an informal parameter, so that CF knows that this form was triggered by the user. – wiesion Sep 30 '14 at 14:11
  • did you update the code in your application as well? i made an error in my first version and switched #url.pickPage# instead of #url.pcount# – wiesion Sep 30 '14 at 14:27
  • in case you want to use POST you need to change the scope of the variables as well. `url` becomes `form` in this case. – wiesion Sep 30 '14 at 14:32
  • it is not that complicated to convert this form to POST, but it seems to me that you're not really familiar with HTTP or CF. Anyways i'll update my answer then you can try again. – wiesion Sep 30 '14 at 14:39
0

Please note that it is a server-side redirect but you can use in cfscript:

location("url", addtoken, statusCode);

or

<cflocation
    url = "URL"
    addToken = "yes|no"
    statusCode = "300|301|302|303|304|305|307">
kwispel
  • 366
  • 1
  • 5
  • Note: the addToken attribute defaults to true, but there's almost no reason it should ever be true. Setting it to true allows someone to share a URL and "hijack" that users session. – Matt Busche Sep 30 '14 at 12:52