By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

Jquery not registering the ready func

P: n/a
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:0p x;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[i].rel.toLowerCase()=='stylesheet')doc.write('<link
type="text/css" rel="stylesheet" href="'+links[i].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>

Oct 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Oct 27, 6:14*pm, "souporpo...@gmail.com" <soup_or_po...@yahoo.com>
wrote:
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:0p x;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[i].rel.toLowerCase()=='stylesheet')doc.write('<link
type="text/css" rel="stylesheet" href="'+links[i].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]
Oct 30 '08 #2

P: n/a
On Oct 27, 6:14*pm, "souporpo...@gmail.com" <soup_or_po...@yahoo.com>
wrote:
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:0p x;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.
Oct 30 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.