Open Document In New Window With iPhone And iPad

 

In most of the Mobile Apps there are situations to open a document like PDF but it become problematic to open such documents as it opens in the same container and the  app that causes the document to be opened no more exists and then you need to run the app again to start working or you might get application error while pressing back button to go back to the app,   the better approach is to open the document in the new window and keep the app in the previous one, here is the code to accomplish it.

public String getUrl(){
    
        String url="";
    //---------write your code and logic here to form url--------    
        
    String script = "<script>window.location.href='"+url+"'</script>";    
    return script;
    }

in the above method you can write your logic to form the url to open a document, the last line of code is returning script that will open the document in the same window and this window will be a new window as we will be triggering it from the client javascript, below is the javascript and html that  leads the document to be opened in the new window, as we have already fired a window.open and the returning url is composed of script and will tells the browser to open in the same container of the newly opened window.

 

<html>
    <head>
        <script>
            function navigateToDocument() {
                    
                    var url = $("#getDocLink").attr("href");
                    window.open(url);
                    $.post(url, function(data) {
                        
                        
                    });
                }    
                
        </script>
    
    </head>
        <body>
        <a class="link" href="javascript:navigateToDocument();"> Click here to open doc </a>
        </body>
</html>
    

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>