0

If i click again the submit button it will overwrite the existing file. Question is how i can force to open a new file and not overwrite the existing file?

<form id="form1" name="templet" onsubmit="window.open('','templet','width=900,height=1000,scrollbars=yes,')" action="/" method="post" target="templet">
  <select name="gotemplet" id="gotemplet" size="1" onChange="javascript:chgAction()" required>
    <option value="">Select an option</option>
    <option data-action="./test2.html" value="down">Service Down</option>
    <option data-action="./test1.html" value="mail">Mail</option>
  </select>
  <input type="submit" value="create template">
</form> 

Here is the script

(function() {
  var form = document.querySelector('#form1'),
    select = form.querySelector('#gotemplet'),
    action = {
      'test1': './test1.html',
      'test2': '. / test2.html ',
    };

  select.addEventListener('change', function() {
    var el = this,
      value = el.value;
    if (action[value]) {
      form.action = action[value];
    }
  }, false);
}());
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
Stefan M.
  • 13
  • 3
  • than do not give it the same name, try _blank instead – epascarello May 09 '18 at 13:34
  • https://stackoverflow.com/questions/14643040/allow-window-open-to-open-new-window-and-not-popup – Alive to die - Anant May 09 '18 at 13:34
  • You could dynamically change the target to be different. Like appending the Date.now() value to it or something any time the select change happens. – Taplar May 09 '18 at 13:36
  • Possible duplicate of [Allow window.open to open new window and not popup](https://stackoverflow.com/questions/14643040/allow-window-open-to-open-new-window-and-not-popup) – Calvin Nunes May 09 '18 at 13:37
  • You may using Android browser for that – AA Shakil May 09 '18 at 14:01
  • _blank is not the solution otherwise it will open directly into the iframe and it open a _blank page, what about to set an ID? – Stefan M. May 09 '18 at 14:28
  • @Taplar sound intressting could you please made an example? – Stefan M. May 09 '18 at 14:34
  • `form.target = 'templet'+ Date.now()` inside your change handler. This will put the millisecond form of the current time on the target making it unique for the next time you submit the form. – Taplar May 09 '18 at 14:37
  • @Taplar, problem is the same if i submit the existing page will be overwrited. may i understood it wrong.. inside the window.open (.... – Stefan M. May 09 '18 at 14:44
  • Try taking off the onsubmit logic and having just the target logic and see if that changes anything for you. Having a target on the form will cause it to open in a new window already, so having the onsubmit do a new window also is slightly redundant at that point. – Taplar May 09 '18 at 14:48

1 Answers1

1

Submitting a form will always refresh a current page. If you want to open a new window, you need to call window.open as shown in following code snippet.

Please copy and run this code locally, "Run Code Snippet" is not supporting window.open

(function() {
  var form = document.querySelector('#form1'),
    select = form.querySelector('#gotemplet'),
    createBtn = form.querySelector('#createBtn'),
    action = {
      'down': './test1.html',
      'mail': './test2.html ',
    };
  
  var selectedAction = '';
  
  select.addEventListener('change', function() {
      var el = this,
      value = el.value;
      if (action[value]) {
        selectedAction = action[value];
      } else {
  selectedAction = "";
   }
   
  }, false);
  
  createBtn.addEventListener('click', function() {
    if(selectedAction != '') {
    window.open(selectedAction,'_blank','width=900,height=1000,scrollbars=yes,')
    }
  });
}());
<form id="form1" name="templet">
    <select name="gotemplet" id="gotemplet" size="1" required>
   <option value="">Select an option</option>
   <option value="down">Service Down</option>
   <option value="mail">Mail</option>
    </select>
    <input type="button" id="createBtn" value="create template">
</form>
Santosh Sawant
  • 265
  • 2
  • 9
  • _blank is not the solution otherwise it will open directly into the iframe and it open a _blank page, what about set an id? – Stefan M. May 09 '18 at 14:29