Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Codes works with IE, but not with Firefox. 1

Status
Not open for further replies.

lcs01

Programmer
Aug 2, 2006
182
US
Hi, Experts,

I have written a piece of perl/javascript codes listed below (a bit long, sorry. But it is portable to your local machine if you would like to do a test run):

Code:
#!/usr/local/bin/perl

use strict;
use CGI qw/:all/;

local $| = 1;

my $title = "Test";
my $dilimiter = '__';
my $actionCode = 'myperl.pl';

my %clstIDName = 
(
  '0', '--Please Select a Cluster--',
  '1', 'ClusterName1',
  '2', 'ClusterName2',
  '3', 'ClusterName3',
);

my %clstIDCaseIDName =
(
   '1'=>{'11'=>'CaseName11', '12'=>'CaseName12', '13'=>'CaseName13'},
   '2'=>{'21'=>'CaseName21', '22'=>'CaseName22', '13'=>'CaseName23'},
   '3'=>{'31'=>'CaseName31', '32'=>'CaseName32', '13'=>'CaseName33'},
);

my $jscode = buildJSCode(\%clstIDName,\%clstIDCaseIDName,$dilimiter);

print header;
print 
      start_html(-title=>"$title", -script=>$jscode),
      start_multipart_form(-action=>"$actionCode",
        -name=>'theForm',
        -onSubmit=>'return checkForCaseSelection(theForm.caseId);'),

      table({-align=>'center'},
        Tr(td({-align=>'right'}, 'Cluster: '),
          td({-align=>'left'},
            popup_menu(-name=>'clusterId',
              -values=>[sort { $clstIDName{$a} cmp $clstIDName{$b} } keys %clstIDName],
              -labels=>\%clstIDName,
              -default=>'0',
              -override=>1,
              -onChange=>'showCaseListForCluster(theForm.clusterId, theForm.caseId);'))),
        br(),
        Tr(td({-align=>'right'}, 'Case: '),
          td({-align=>'left'},
            popup_menu(-name => 'caseId',
              -values => []))),
        br(),
        Tr(td({-align=>'center',-colspan=>'2'}, (submit(-value=>'Upload'))))),
      end_form(),
      end_html;
exit 0;

sub buildJSCode {
  my ($clstIDNameHashRef,$clstIDCaseIDNameHashRef,$dilimiter) = @_;
  my $jscode = "var caseListByCluster = new Array();\n";
  $jscode .= "var dlmter = '$dilimiter';\n";
  foreach my $clID (keys(%{$clstIDNameHashRef})) {
    if($clID > 0) {
      my @caseIDArray4ThisCluster = keys(%{$clstIDCaseIDNameHashRef->{$clID}});
      my @tmp;
      foreach my $cID (keys(%{$clstIDCaseIDNameHashRef->{$clID}})) {
        my $caseName = CGI::escape($clstIDCaseIDNameHashRef->{$clID}->{$cID});
        push @tmp, "'$caseName$dilimiter$clID$dilimiter$cID'";
      } 
      my $tmp = join ",", sort(@tmp);
      $jscode .= "caseListByCluster[\"$clID\"] = new Array($tmp);\n";
    } # end of if($clID > 0) {
  } # end of foreach my $clID (keys(%{$clstIDNameHashRef})) {
  $jscode .= "\n";
  $jscode .= <<EO_JS;
function showCaseListForCluster(clusterIdObj, caseIdObj) {
  if (clusterIdObj.selectedIndex > 0) {
    var clusterID = clusterIdObj.value;
      var thisArrLength = caseListByCluster[clusterID].length;
      caseIdObj.disabled = false;
      caseIdObj.options[0] = new Option('--Please Select a Case--', '0', true, true);
      for(var i = 0; i < thisArrLength; i++) {
        var j = i + 1;
        var cNameClIdCId = caseListByCluster[clusterID][i];
        var cNameClIdCIdArr = cNameClIdCId.split(dlmter);
        var caseName = cNameClIdCIdArr[0];
        var len = cNameClIdCIdArr.length;
        var caseID = cNameClIdCIdArr[len-1];
        caseIdObj.options[j] = new Option(unescape(caseName), caseID);
      }
  } // end of if (clusterIdObj.selectedIndex > 0) {
  else {
    caseIdObj.options.length = 0;
    caseIdObj.disabled = true;
  }
}

function checkForCaseSelection(caseSelBox) {
  if (caseSelBox.disabled || caseSelBox.selectedIndex < 1) {
    alert('You must select a case to proceed.');
    return false;
  }
  return true;
}
EO_JS
  return $jscode;
}

By design, if you pick a cluster name, a list of corresponding case names are displayed. Then if you click 'reload' or hit 'F5' key, in IE, the cluster name backs to the default value 'Please Select a Cluster' and the case name list becomes blank. This is expected!

However, in Firefox, after you click reload, the cluster name STAYS the SAME as you selected before while the case name list becomes blank. This is WRONG!

How can I make it work in Firefox in the same way as in IE?

Many thanks for your kind advices.
 
Pop up your javascript console before displaying the page. It will tell you where the errors are on your page. Then trace backwards to your perl script.
 
To Billy,

I actually tried using the client-side code and it did not work with IE either. That's why I did not bother post the html/js code.
 
Hi

lcs01 said:
However, in Firefox, after you click reload, the cluster name STAYS the SAME as you selected before while the case name list becomes blank. This is WRONG!
Some time ago on another forum a guy asked the reverse : why Explorer does not keep the selection, just like FireFox does. Why do you think it is wrong just because does not happen as you wish ? The only solution I see is to set it explicitly with JavaScript.

Feherke.
 
feherke said:
The only solution I see is to set it explicitly with JavaScript.

Thank you for your reply. Could please show me the solution? Thanks again.
 
xwb said:
Pop up your javascript console before displaying the page.

I did and saw no any errors/warnings/Messages from the console at all!
 
Here is the client side code. And this time, it does not work with either IE or Firefox!!

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
	PUBLIC "-//W3C//DTD XHTML Basic 1.0//EN"
	"[URL unfurl="true"]http://www.w3.org/TR/xhtml-basic/xhtml-basic10.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] lang="en-US"><head><title>Test</title>
<script language="JavaScript" type="text/javascript">
<!-- Hide script
//<![CDATA[
var caseListByCluster = new Array();
var dlmter = '__';
caseListByCluster["1"] = new Array('CaseName11__1__11','CaseName12__1__12','CaseName13__1__13');
caseListByCluster["2"] = new Array('CaseName21__2__21','CaseName22__2__22','CaseName23__2__13');
caseListByCluster["3"] = new Array('CaseName31__3__31','CaseName32__3__32','CaseName33__3__13');

function showCaseListForCluster(clusterIdObj, caseIdObj) {
  if (clusterIdObj.selectedIndex > 0) {
    var clusterID = clusterIdObj.value;
      var thisArrLength = caseListByCluster[clusterID].length;
      caseIdObj.disabled = false;
      caseIdObj.options[0] = new Option('--Please Select a Case--', '0', true, true);
      for(var i = 0; i < thisArrLength; i++) {
        var j = i + 1;
        var cNameClIdCId = caseListByCluster[clusterID][i];
        var cNameClIdCIdArr = cNameClIdCId.split(dlmter);
        var caseName = cNameClIdCIdArr[0];
        var len = cNameClIdCIdArr.length;
        var caseID = cNameClIdCIdArr[len-1];
        caseIdObj.options[j] = new Option(unescape(caseName), caseID);
      }
  } // end of if (clusterIdObj.selectedIndex > 0) {
  else {
    caseIdObj.options.length = 0;
    caseIdObj.disabled = true;
  }
}

function checkForCaseSelection(caseSelBox) {
  if (caseSelBox.disabled || caseSelBox.selectedIndex < 1) {
    alert('You must select a case to proceed.');
    return false;
  }
  return true;
}

//]]> End script hiding -->
</script>
</head>
<body>
<form method="post" action="myperl.pl" enctype="multipart/form-data" 
  onsubmit="return checkForCaseSelection(theForm.caseId);" name="theForm">
<table align="center">
<tr>
  <td align="right">Cluster: </td> 
  <td align="left">
    <select name="clusterId" onchange="showCaseListForCluster(theForm.clusterId, theForm.caseId);">
      <option selected value="0">--Please Select a Cluster--</option>
      <option  value="1">ClusterName1</option>
      <option  value="2">ClusterName2</option>
      <option  value="3">ClusterName3</option>
    </select>
  </td>
</tr> <br /> 
<tr>
  <td align="right">Case: </td> 
  <td align="left"> <select name="caseId"> </select> </td>
</tr> <br /> 
<tr><td align="center" colspan="2">
<input type="submit" name=".submit" value="Upload" /></td></tr>
</table>
</form>
</body>
</html>

Thank you, Billy, for your help.
 
It works!!! Thank you so much, feherke!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top