locked
how-to-use-window-open url-_blank-without-changing-calling-application-url RRS feed

  • Question

  • User1914091530 posted

    Hi,

    How to use window-open url _blank without changing calling application url.

    It should show same application url in the opened window also.

    Thanks & Regards

    Shijo

    Friday, April 10, 2020 6:52 AM

All replies

  • User753101303 posted

    Hi,

    Seems you are looking for https://developer.mozilla.org/en-US/docs/Web/API/Document/location to get the current url. And so try something like:
    window.open(document.location.href,"blank");

    Edit: make sure it make sense to have users having to switch accross tabs, afaik this is discouraged and in particular may not work if you want to support small screens.

    Friday, April 10, 2020 7:09 AM
  • User1914091530 posted

    Hi,

    It does not seems to work

    My ajax call is as below.

                $.ajax({
                    type: "POST",
                    url: "/PurchasedGoodsQC/Details",
                    data: '{PurchaseGoodsPDF:' + JSON.stringify(PurchaseGoodsPDF) + '}',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        debugger;
                        window.open(data.path, '_blank');
                        document.location = 'http://localhost/pdfdocuments';
                    }
                });

    What I need is to open data.path in new browser tab with a different url   'http://localhost/pdfdocuments';

    Thus not showing the entire file path

    Thanks & Regards

    Shijo

    Friday, April 10, 2020 8:12 AM
  • User753101303 posted

    Always tell what happens? Does it hit your breakpoint ? Does it open another tab?  Do you have the correct value in data.path?

    window.open('https://asp.net','_blank'); does show the ASP.NET site in a new tab (at least with a desktop browser).

    Not directly related but I always wonder when seeing an Ajax request followed anyway by a redirect (which basically make using Ajax not that worth).

    Also sometimes it's best to first briefly tell about the final goal rather than to ask about a technical solution that may not be a correct approach for this goal.

    Are you trying to show (not download) a PDF file in a new tab?

    I often prefer to let the user download or open the PDF file. In this case you don't have to use a new window or tab because the browser knows that a download dialog should be shown rather than being shown in the current browser window.

    Or it seems also you are perhaps asking about showing some other content while still showing in the address bar the same url than another tab that shows a different content ????

    Friday, April 10, 2020 11:17 AM
  • User1914091530 posted

    Hi,

    I am trying to show pdf file in new tab and i am able to do it using window.open('url','_blank');

    I just don't need to show the file path in url location.

    I want to change the url of the opened tab to say 'http://localhost/pdfdocuments'. This is requirement from customer.

    Thanks & Regards

    Shijo

    Friday, April 10, 2020 11:31 AM
  • User753101303 posted

    This is not how the web works. By design the address bar does show the location of the retrieved ressource. You could: 
    - try the 3rd parameter shown at https://developer.mozilla.org/en-US/docs/Web/API/Window/open and in particuliar location=no to hide the address bar (but it should show as a popup rather than a new tab)
    - another option could be to use an iframe
    - or maybe https://developer.mozilla.org/en-US/docs/Web/API/History/pushState but it might cause other issues.
    - maybe something else I didn't thought

    Note though that all this is just a dumb way to blindly match a "don't show the url on screen" requirement but won't prevent a savvy user to easily retrieve this information (using F12 Network and looking for http requests returning PDF content for example).

    I would rather discuss with my customer to understand exactly what he tries to prevent and suggest the best solution I can (if possible at all, I saw once who wanted basically to brainwash users when closing his site or another one that wanted to encrypt even controller and action names for some mysterious reason).

    Edit: for example it's common to serve a document using code so that it is only available to given users or maybe for a limited time. AFAIK you have solutions to prevent forwarding a document to someone else (if this is the requirement) but it usually needs a whole DRM infrastructure.

    Friday, April 10, 2020 12:20 PM
  • User-474980206 posted

    Then the url should be what you want and use a post to send the file name rather than a get parameter.  Just use a Hidden form with a target=“_blank” rather than window.open()

    Friday, April 10, 2020 3:42 PM
  • User-17257777 posted

    Hi shijostephen,

    I think you can just define a controller action to return and display a pdf file, it will not show the full path of the file, so, you don't need to replace the url. Below is my test demo.

    <button id="btn" >Show PDF</button>
    
    @section scripts{
        
        <script>
            $("#btn").on('click', function () {
                window.open('https://localhost:44364/Pdfdocuments', '_blank');
            })
        </script>
        
    }
    public class PdfdocumentsController : Controller
        {
            // GET: Pdfdocuments
            public FileContentResult Index()
            {
                var fullPathToFile = Server.MapPath("/App_Data/mypdf.pdf");
                var mimeType = "application/pdf";
                var fileContents = System.IO.File.ReadAllBytes(fullPathToFile);
    
                return new FileContentResult(fileContents, mimeType);
            }
        }

    Result:

    Best Regards,

    Jiadong Meng

    Monday, April 13, 2020 6:11 AM