0

I am trying to open a new window with the below code. It opens a new window but it has the url as "about:blank". How to change the this url and give a custom url.

private native void openPrintWindow(String contents) /*-{
    var printWindow = window.open("", "PrintWin", false);
    printWindow.document.open("text/html","replace");
    if (printWindow && printWindow.top) {
        printWindow.document.write(contents);

    } else {
        alert("The print feature works by opening a popup window, but our popup window was blocked by your browser.  If you can disable the blocker temporarily, you'll be able to print here.  Sorry!");
    }
}-*/;

1 Answers1

0

It is empty, because first parameter of window.open method is an empty string. Check some examples here. So it should be something like this:

window.open("https://stackoverflow.com", "PrintWin", false);

From Your code I see You want to open a new window by custom URL with some HTML content inside. You cannot do it this way. If You put some URL, the browser will try to open this URL by making a GET request.

Solution to what You want to achieve is to do it more-or-less the MVC way (please note it is NOT a fully correct MVC solution, just a guidance):

  1. Before You open the window, You need to store a content somewhere (best option is on a server side, but there is also a way to store it on a client side)
  2. Create a new page, accessible via Your custom URL (either a simple HTML or a service, up to Your needs).
  3. You need to write some code in this new page which will retrieve Your content (stored previously somewhere) and present it in this newly opened window.
zolv
  • 1,720
  • 2
  • 19
  • 36
  • Thanks for the response. Can you provide information on creating a new page with custom URL - simple HTML way. I need everything to be on client side. – Jaikishan Ventrapragada May 08 '19 at 18:27
  • For a super-simple usecase, You can use cookies to store some data. [Check here](https://www.w3schools.com/js/js_cookies.asp), especially the part with reading and writing functions - You can use them. But usually it shouldn't be done this way. Much better it is to store it in a persistent storage on a server side using some framework. THis is a loooong topic :) – zolv May 08 '19 at 18:35
  • 1
    Is this what you are looking for? https://stackoverflow.com/questions/2109205/open-window-in-javascript-with-html-inserted – Adam May 08 '19 at 19:54