Question

I'm using the following code snip to open pdf files from a SP2013 document library in a new window.

I'm hoping someone might know how I can update the code to make the resulting new window open in the centre of the screen?

    <script language="javascript" type="text/javascript">
    $(document).ready(function(){

        if(window.location.href.toUpperCase().indexOf("/xxxx/SITEPAGES/")>0)
    {
        
        //on category click
        $( ".ms-gb" ).click(function() {
    
        
        setTimeout(function(){ openLinkinNewWindowForms(); }, 1000);
        });
    }
 });
$(window).on('load', function() {
setTimeout(function(){ openLinkinNewWindowForms(); }, 1000);
});

function openLinkinNewWindowForms()
{
    //alert("clicked");
        
        //$("#onetidDoclibViewTbl0").find("a.ms-listlink.ms-draggable").each(function(){    

$(".ms-listlink").each(function(){  
//alert("inside function");     
    
if($(this).attr("href"))
{
    var targetValue=$(this).attr("href");

    //if(targetValue.indexOf(".aspx")>0)
        //{
            var anchorTag = $(this)[0];
            var linkUrl=anchorTag.href;
            anchorTag.removeAttribute("href");
            var customClick =  "window.open('"+linkUrl+"','_blank','resizable=1,scrollbars=yes,height=600,width=800,modal=yes,alwaysRaised=yes')";
                        //window.resizeTo( 1200,800 );
            anchorTag.setAttribute("onclick", customClick);
            anchorTag.setAttribute("style","cursor:pointer");
        //}
}
    });

}   

</script>

Thanks,

Matt

Was it helpful?

Solution

You can try opening the window with top and left parameters as shown below

<!DOCTYPE html>
<html>
<body>

<p>Click the button to open a new window with some specifications.</p>

<button onclick="myPopup('https://www.w3schools.com', 'web', 1100, 500);">Open Qries.com</button>

<script>

function myPopup(myURL, title, myWidth, myHeight) {

            var left = (screen.width - myWidth)/2;
            var top = (screen.height - myHeight)/4;           
            
            var myWindow = window.open(myURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=' + myWidth + ', height=' + myHeight + ', top=' + top + ', left=' + left);
         }



</script>

</body>
</html>

You can also check it here and geeks

Updating function openLinkinNewWindowForms() to open pdfs in a new windows.

Changes: New lines added and one existing line has been commented out. Replace the entire function with the script provided here. It has been verified with Chrome


function openLinkinNewWindowForms()
{
    //alert("clicked");
        
        //$("#onetidDoclibViewTbl0").find("a.ms-listlink.ms-draggable").each(function(){    

$(".ms-listlink").each(function(){  
//alert("inside function");     
    
if($(this).attr("href"))
{
    var targetValue=$(this).attr("href");

    //if(targetValue.indexOf(".aspx")>0)
        //{
            var anchorTag = $(this)[0];
            var linkUrl=anchorTag.href;
            anchorTag.removeAttribute("href");

            //**Begin new Lines of Code**
           
            var width=800;
            var height=600;
            var left = (screen.width - width)/2;
            var top = (screen.height - height)/4;
            var customClick =  "window.open('"+linkUrl+"','_blank','resizable=1,scrollbars=yes,height=600,width=800,modal=yes,alwaysRaised=yes" + ", top=" + top + ", left=" + left +"')";
           
           //**End of new Lines of Code**
            
            //**commented out this line
           //var customClick =  "window.open('"+linkUrl+"','_blank','resizable=1,scrollbars=yes,height=600,width=800,modal=yes,alwaysRaised=yes')";
                        //window.resizeTo( 1200,800 );
            anchorTag.setAttribute("onclick", customClick);
            anchorTag.setAttribute("style","cursor:pointer");
        //}
}
    });

}   


Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top