Jquery not registering the ready func

Discussion in 'Javascript' started by souporpower, Oct 27, 2008.

  1. souporpower

    souporpower Guest

    Hi All
    I am trying to print some HTML using JQUERY. I am posting the code. I
    don't
    see the alert. It seems as though the function is not registered when
    the
    document is loaded. Can someone please clarify what I am doing wrong?
    BTW, I am using IE8 and Chrome to test. Sorry I can't place the code
    in a web site.
    Thanks for your help

    <html>
    <head>
    <script type="text/javascript" src="../../resources/js/
    jquery-1.2.6.js"> </script>
    <script language="javascript" type="text/javascript"> $
    (document).ready(function(){ $('.mylink').click(function()
    { $.jPrintArea('#tabularData') }); });

    jQuery.jPrintArea=function(el){

    alert("hello");

    var iframe=document.createElement('IFRAME');var doc=null;

    $(iframe).attr('style','position:absolute;width:0px;height:
    0px;left:-500px;top:-500px;');

    document.body.appendChild(iframe);

    doc=iframe.contentWindow.document;

    var links=window.document.getElementsByTagName('link');

    for(var i=0;i<links.length;i++)

    if(links.rel.toLowerCase()=='stylesheet')doc.write('<link
    type="text/css" rel="stylesheet" href="'+links.href+'"></link>');

    doc.write('<div class="'+$(el).attr("class")+'">'+$(el).html()+'</
    div>');

    doc.close();

    iframe.contentWindow.focus();

    iframe.contentWindow.print();alert('Printing...');//
    wait(1);document.body.removeChild(iframe);}

    </script>
    </head>

    <body>
    <div id="tabularData">
    ....
    </div>
    <a href="#" class="mylink" name="mylink">Print this Table</a>
    </body>
    </html>
     
    souporpower, Oct 27, 2008
    #1
    1. Advertisements

  2. souporpower

    David Mark Guest

    Problem with the ready method in jQuery? Who could have predicted
    that?
    You are using JQUERY (sic).
    ^^^^^^

    There's your mistake. Clear as day.

    Why are you initializing the doc variable to null? You assign a
    document object to it three lines later.
    This is what I am talking about. How many function calls is that? I
    will just estimate 1000, several of them will trigger document re-
    flows. All could have been avoided like this:

    var style = iframe.style;
    style.position = 'absolute';
    style.width = style.height = '0px';
    style.left = style.top = '-500px';

    Is that any harder to read than your mess?

    Now your average jQuery "programmer" who is hopelessly conditioned to
    write inefficient code will cry foul at the quadrupling of the lines
    of code. Of course, as minification is the rule these days
    (especially among library users), then the term "lines" is
    meaningless. Count the characters. Then minify and recount. Aha.

    Final tally.

    Function calls: 0
    Re-flows: 0
    Objects created: 0

    Nobody really knows what the tallies are on yours, but God knows they
    aren't good. Lets say 500 function calls + a few re-flows. The
    browser has to get the resources to do this from somewhere, so the
    other programs running on the computer are short-changed. All because
    you didn't want to learn browser scripting.
    Bravo. If you had used a jQuery object to do that, it would have
    added at least a few dozen more function calls (and created and
    discarded another $ object.)


    Strange. It is like you abandoned jQuery half way through.
    Spoke too soon. This is deranged, even for a jQuery script. That "$"
    does not signify a variable. Stop treating it like one. Better yet,
    stop using it completely.
    Give it up. The world needs static pages too.

    [snip]
     
    David Mark, Oct 30, 2008
    #2
    1. Advertisements

  3. souporpower

    David Mark Guest

    Sorry "souporpower", the re-flows don't apply as you hadn't appended
    the iframe yet. I see this pattern used all the time where it
    triggers dozens of needless re-flows. Yours is not quite that bad.
    Bad enough though.
     
    David Mark, Oct 30, 2008
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.