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]