Jquery not registering the ready func

S

souporpower

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>
 
D

David Mark

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

Problem with the ready method in jQuery? Who could have predicted
that?
the
document is loaded. Can someone please clarify what I am doing wrong?

You are using JQUERY (sic).
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>
^^^^^^

There's your mistake. Clear as day.

<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;

Why are you initializing the doc variable to null? You assign a
document object to it three lines later.
$(iframe).attr('style','position:absolute;width:0px;height:
0px;left:-500px;top:-500px;');

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.
document.body.appendChild(iframe);

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.)
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>');


Strange. It is like you abandoned jQuery half way through.
doc.write('<div class="'+$(el).attr("class")+'">'+$(el).html()+'</
div>');

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.
doc.close();

iframe.contentWindow.focus();

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

Give it up. The world needs static pages too.

[snip]
 
D

David Mark

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);

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top