0

I tried apply jquery datepicker on a newly opened blank window (opened with window.open).

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script src="https://code.jquery.com/jquery-3.6.0.js"></script>
  <script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
</head>
<body>
 
 
 
</body>
</html>

  <script>
  const button = document.createElement('button');
  button.id = 'button';
  document.body.appendChild(button);
  
  $('#button').on('click', function() {

      newWindow = window.open('', '_blank', 'width=700, height=1000');
      var div = newWindow.document.createElement('div');
      div.style.textAlign = "center";
      div.id = "div_riaszt_kuldes"; 
      newWindow.document.body.appendChild(div);
      input2 = $('<input id="datepicker" type="text">', [,newWindow.document.body]);
      $(input2).appendTo(newWindow.document.body);
      $(input2).datepicker();
  }); 
  
  
  </script>

When I call datepicker, the following error occurs:

jquery-3.6.0.js:6401 Uncaught TypeError: Cannot read properties of null (reading 'defaultView') at getStyles (jquery-3.6.0.js:6401:33) at curCSS (jquery-3.6.0.js:6585:25) at Function.css (jquery-3.6.0.js:6996:10) at jquery-3.6.0.js:7145:12 at access (jquery-3.6.0.js:4203:15) at jQuery.fn.init.css (jquery-3.6.0.js:7127:10) at datepicker_getZindex (jquery-ui.js:7333:19) at HTMLInputElement._showDatepicker (jquery-ui.js:8130:31) at HTMLInputElement.dispatch (jquery-3.6.0.js:5430:27) at elemData.handle (jquery-3.6.0.js:5234:28)

The elem.ownerDocument is a null object specifically.

However, when I opened the new window as _parent, everything works like a charm.

6
  • Not sure if it actually set up to correctly handle things when used like this, across "document boundaries." You might have to inject jquery & jqueryUI into the popup first, and then initialize the datepicker "in" there, the normal way.
    – CBroe
    Commented Feb 21 at 8:11
  • could you please provide stackblitz. Commented Feb 21 at 8:38
  • you mean what output generated running this code on stackblitz? there was no error but nothing happened clicking on the input object Commented Feb 21 at 13:23
  • URL of the project: stackblitz.com/edit/javascript-hn9iop?file=index.js Commented Feb 21 at 13:27
  • Does this help? Commented Feb 21 at 21:58

0