0

I have a javascript function written as follows which works just fine in Firefox for downloading given txt content.

  self.downloadURL = function (url) {
        var iframe;
        iframe = document.getElementById("hiddenDownloader");        
        if (iframe === null) {
            iframe = document.createElement('iframe');
            iframe.id = "hiddenDownloader";
            iframe.style.display = "none";
            document.body.appendChild(iframe);
        }
        iframe.src = url;
    }

but it does n't work fine with IE 9 due to some reasons. So i tried to convert into equivalent jquery because jquery has compatibility with all the browsers.

here is the jquery equivalent of the same function:

   self.downloadURL = function (url) {
        var iframe;
        iframe = $("#hiddenDownloader");        
        if (iframe === null) {
            iframe = $('<iframe></iframe>');
            iframe.id = "hiddenDownloader";            
            $("#hiddenDownloader").css("display", "none");
            $(document.body).append(iframe);
        }
        iframe.src = url;
    }

But now it does n't work in both the browsers. Please help letting me know what am i doing wrong.

2
  • Do you get any particular error message? Commented Sep 5, 2013 at 12:39
  • I dont get any particular error. it just does not respond in IE 9 Commented Sep 5, 2013 at 13:34

6 Answers 6

9

Your problem is in:

iframe = $('<iframe></iframe>');
iframe.id = "hiddenDownloader";       

iframe refers to a jQuery Object not a DOM Node. You will have to use .prop to set the id:

iframe = $('<iframe></iframe>');
iframe.prop('id', "hiddenDownloader");       

And also you have the same problem here:

if (iframe === null) {

Where you will need the check the length of iframe:

if (iframe.length === 0) {

And again at iframe.src = url; Maybe you can figure this one out:

iframe.attr('src', url);

But why would you convert vanilla JavaScript to jQuery?

Sign up to request clarification or add additional context in comments.

8 Comments

iframe.length === 0 (or maybe just !iframe.length) would be better than testing against null IMHO.
iframe.length === 0 Sure think :)
@NULL No need for === 0. if (iframe.length) will do just fine :)
@Archer I know. Its because 0 is falsy as a lot of other things is in JavaScript (undefined, null, "", etc). But === 0 is more clear for not so skilled programmer :)
Good point, and I've just realised that it had already been said and I also missed a !. #Hangsheadinshame
|
0

Seems like a strange way to download a file.

In any case, this probably works the same was as the original:

if (!iframe.get(0)) {
  ...
}

... and the id property as another poster mentions.

Comments

0
iframe = document.getElementById("hiddenDownloader");          
iframe = $("#hiddenDownloader");        

These lines aren't equivalent. The second one create a jQuery object, so the check for null will never equate to true

Comments

0

This link may help u:

http://www.sitepoint.com/forums/showthread.php?743000-IE9-Iframes-DOCTYPES-and-You

or u can add this to the top:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Comments

0
  • Replace iframe.src = url; with iframe.attr("src",url + "?"+ Math.random()); to prevent caching issue in IE9.
  • Replace iframe.id = "hiddenDownloader"; with iframe.attr("id","hiddenDownloader");
  • Replace if (iframe === null) { with if (iframe.length === 0) {

1 Comment

@Alagesan Palani: this could be the caching problem in IE9, try my updated answer url + "?"+ Math.random()
0

Try with:

iframe = $("#hiddenDownloader")[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.