Pregunta

I am trying to create a modal dialog on click of a hyperlink file column(SharePoint 2013).I am able to open the Modal dialog in an Iframe But it's happening only after the file download. I don't want my file to get downloaded but just need to open in a popup. Please find my code below:

function openDialog( currenturl,Filename ){ 
SP.UI.ModalDialog.showModalDialog({ 
url: currenturl, 
width: 800, 
height: 1500, 
allowMaximize: true,
showClose: true,
title: Filename
 });
 } 

//Happens onclick of the hyperlink_button inside a column:

     function FilePopup(FileValue){  
    
        
         var ActualUrl = $(FileValue)[0].href; 
         var ID = ActualUrl.split("/")[10];
         var Filename =$(FileValue)[0].text;
         var ActualFilename= Filename.split(".")[0];
         var currentURL =  "https:Mysiteurl";

       //Calling ModalDialog
       openDialog(currentURL,ActualFilename); 
       }

Source

   <a href="Mysiteurl/file.pdf" target="_blank" 
   onclick="FilePopup(this)">test.pdf</a>
¿Fue útil?

Solución

The easiest way would be to just return false; in your click handler:

<a href="Mysiteurl/file.pdf" target="_blank" onclick="FilePopup(this); return false;">test.pdf</a>

Alternatively, since you are using jQuery, you could use it to attach the click handler instead of putting it inline in the HTML, so you can get easy access to the click event there, and prevent it's default behavior:

$(document).ready(function(){
    $('a').click(function(event) {
        // stop the download
        event.preventDefault();

        // do the rest of your stuff to open the dialog
        var ActualUrl = $(this)[0].href; 
        var ID = ActualUrl.split("/")[10];
        var Filename =$(this)[0].text;
        var ActualFilename= Filename.split(".")[0];
        var currentURL =  "https:Mysiteurl";

       //Calling ModalDialog
       openDialog(currentURL,ActualFilename); 
    });
});

// no inline "onclick" on your anchor
<a href="Mysiteurl/file.pdf" target="_blank">test.pdf</a>
Licenciado bajo: CC-BY-SA con atribución
scroll top