3

How can I change this code to take advantage of jQuery?

function download(elm) {
    var iframe = document.createElement("iframe");
    var param = elm.innerHTML; //$get("filedownload").innerHTML;        
    //iframe.src = "GenerateFile.aspx?filename=386c1a94-fa5a-4cfd-b0ae-40995062f70b&ctype=application/octet-stream&ori=18e73bace0ce42119dbbda2d9fe06402.xls";// + param;
    iframe.src = "GenerateFile.aspx?" + param;

    iframe.style.display = "none";

    document.body.appendChild(iframe);
}
0

1 Answer 1

10

It would look like this:

function download(elm) {
  $("<iframe />", { src: "GenerateFile.aspx?" + elm.innerHTML })
    .appendTo("body").hide();
}

This is the jQuery 1.4+ $(html, props) syntax, for older versions it would look like this:

function download(elm) {
  $("<iframe />").attr("src","GenerateFile.aspx?" + elm.innerHTML)
    .appendTo("body").hide();
}

Past the creation .appendTo() appends the element you created to the selector passed in ("body"), and .hide() covers the display: none; styling.

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

3 Comments

don't forget iframe.style.display = 'none';
Thanks, I didn't know about $(html, props) syntax before. That would be pretty useful.
This is why i love jQuery - 7 lines of ugly javascript turned into one chained line of sexiness. :)

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.