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
Solved

coldfusion, javascript onclick url update

Posted on 2016-11-10
4
60 Views
Last Modified: 2016-11-11
I have a page with dropdown. When dropdown selection is made, the url updates. The following code works on IE, but not on Chrome/Firefox.  What am I missing? thanks.

<script>
function nav()
   {
   var w = window.pgnames.selectedIndex;
   var val = window.pgnames.options[w].value; 
   var st = /&pg/;
   var curl = document.URL.search(st);

   if(curl == -1)
                var nurl = document.URL;
   else 
                var nurl = document.URL.substr(0, curl);
   window.location.href = nurl + "&pg="+ val;
   }

</script>

Open in new window



<select name="pgnames" align="absmiddle" onChange="nav()">
<option value="">--- By Practice Group ---</option>
<cfoutput query="getPGNames"><option value="#pgnumber#"<cfif isdefined("pg")><cfif pg EQ #pgnumber#> selected</cfif></cfif>>#PracticeGroup#</option></cfoutput>
</select>

Open in new window

0
Comment
Question by:Member_2_7971128
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 500 total points
ID: 41882671
 var w = window.pgnames.selectedIndex;
  var val = window.pgnames.options[w].value; 

Open in new window


Instead of that, use DOM syntax.  Give the select list an "ID"

         <select id="pgnames" name="pgnames" ....>

.. and get the selected value with this:

       var list = document.getElementById("pgnames");
       var val  = list.options[list.selectedIndex].value;

Open in new window

1
 
LVL 55

Expert Comment

by:Julian Hansen
ID: 41882780
You can make your code work by passing the control to the nav function like so
NOTE this passed as parameter in nav()
HTML
<select name="pgnames" align="absmiddle" onChange="nav(this)">
<option value="">--- By Practice Group ---</option>
<cfoutput query="getPGNames"><option value="#pgnumber#"<cfif isdefined("pg")><cfif pg EQ #pgnumber#> selected</cfif></cfif>>#PracticeGroup#</option></cfoutput>
</select> 

Open in new window

JavaScript
<script>
function nav(sel)
{
  var w = sel.selectedIndex;

  var val = sel.options[w].value;
  var st = /&pg/;
  var curl = document.URL.search(st);

  if(curl == -1) {
    var nurl = document.URL;
  }
  else {
    var nurl = document.URL.substr(0, curl);
  }
  
  window.location.href = nurl + "&pg="+ val;
}
</script>

Open in new window

0
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 500 total points
ID: 41882784
While both options work, I'd go with Julian's suggestion as it's simpler.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41883059
you can short your code a bit more :
<select name="pgnames" align="absmiddle" onChange="nav(this.value)">
<option value="">--- By Practice Group ---</option>
<cfoutput query="getPGNames"><option value="#pgnumber#"<cfif isdefined("pg")><cfif pg EQ #pgnumber#> selected</cfif></cfif>>#PracticeGroup#</option></cfoutput>
</select> 

Open in new window


<script>
function nav(val)
{
  var st = /&pg/;
  var curl = document.URL.search(st);

  if(curl == -1) {
    var nurl = document.URL;
  }
  else {
    var nurl = document.URL.substr(0, curl);
  }
  
  window.location.href = nurl + "&pg="+ val;
}
</script>

Open in new window

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 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