Change iFrame src on ajax complete
using jQuery's $.ajax() I want to retrieve some information from the server, and then, based on that info, change the src attribute of an iframe.
Something like:
$.ajax(
{
url: "someUrl.aspx/getInfo",
dataType: "json",
data: "{'data':{'data1':'data1'}}",
type: "post",
contentType: "application/json; ch开发者_开发技巧arset=utf-8",
complete: function(data, stat) {
if (stat == "success" )
{
var src = JSON.parse(jsondata.responseText).d.src
$('#myframe').attr("src",src);
}
}
}
);
The page I am trying to load is returning a PDF file, so the goal is to show the user the dialog to choose between downloading or opening that file. On IE7 and 8 the browser is showing the info bar with this message : "To help protect your security, Internet Explorer blocked this site from downloading files to your computer. Click here for options."
Changing the iframe's src attribute OUTSIDE the ajax call works fine and the dialog to choose between opening or saving the PDF is shown.
Any workarounds to avoid the info bar showing? Thanks in advance.
You could try this if you don't want to use jQuery
document.getElementById("myframe").src=src;
try this wit jQuery
var iframe = $('#myframe');
$(iframe).attr('src', src)
In order to open a PDF try this
window.open(src);
will open the pdf in a new window...but the info bar cannot be avoided....
An alternate approach that avoids both an iframe and the info bar is to simply not open the PDF automatically. In the call AJAX callback, create a regular link to the PDF file so the user can click on it directly. This should prevent IE from flagging the download.
I am not sure if it'll work, but you can try this:
var alterIframe = function(src){
$('#myframe').attr("src",src);
};
$.ajax(
{
url: "someUrl.aspx/getInfo",
dataType: "json",
data: "{'data':{'data1':'data1'}}",
type: "post",
contentType: "application/json; charset=utf-8",
complete: function(data, stat) {
if (stat == "success" ) {
data = $.parseJSON(data);
setTimeout('alterIframe("' + data.src + '")',300);
}
}
}
);
I hope this helps you ;)
精彩评论