javascriptwindow.openonload-eventselect-options

JS onload function not working using target _self or _top [codeigniter3] on select option element


I'm creating view page named page "review" in codeigniter. and set the input value using JS onload function.

I wana page to load in same widow. but when I'm using window.open(url,'_self') or window.open(url,'_top') in JS code, the input value in element <select> on page "review" are't set. however, <select> value are set or working when i'm using window.open(url,'_blank')

what should i do.?

some view code was error :

<div>
  <label>gender</label>
     <select id="gender" name="gender" required>
      <option value="">- select -</option>
      <option value="2">male</option>
      <option value="3">female</option>
    </select>
 </div>

my JS code placed in footer page :

$(document).ready(function(){
    $('.reviewBtn').click(function(){
     var dt0 = $(this).data('trans_code');
     var dt1 = $(this).data('status');
     var dt2 = $(this).data('gender');
     var pg1 = window.open('<?php echo base_url()?>trans/review/'+dt0,'_blank'); // input value set
     // var pg1 = window.open('<?php echo base_url()?>trans/review/'+dt0,'_self'); // input value not set
      pg1.onload = function(){
        if ( dt1 == "2" ) { // rejected
          pg1.document.getElementById('gender').value = dt2;
          pg1.document.getElementById('gender').required = false; // user could change
        } else {
          pg1.document.getElementById('gender').value = dt2;
          // user couldn't change
         }
      }
    });
  });

my controller

public function review($trans_code){ 
 $data['value']= $this->model->get_data($trans_code)->result(); // value = var to set another input value in review page
 $this->load->view('header');
 $this->load->view('review',$data);
 $this->load->view('footer');
}

Solution

  • finally I solve this problem. I break JS code in to two part. first I move JS script window.onload to a new page review. I retrieve gender value using php code, foreach($value as $dt) to obtained value from modal result

    window.onload = function(){
      var dt0 = <?php echo $dt->trans_status?>;
      var dt1 = <?php echo $dt->cust_gender?>;
        if ( dt0 == "2" ) { // rejected
          document.getElementById('gender').value = dt1;
          document.getElementById('gender').required = false; // user could change
        } else {
            document.getElementById('gender').value = dt2;
              // user couldn't change
         }
    }
    

    second, modified JS code in footer

    $('.reviewBtn').click(function(){
         var dt0 = $(this).data('trans_code');
         window.open('<?php echo base_url()?>trans/review/'+dt0,'_self'); 
    });
    

    and <select> value was set successfully.