473,395 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Jquery not registering the ready func

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
2 4535
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: mikeh3275 | last post by:
I'm new to developing in jQuery, so I'm probably doing this wrong. I'm loading a form into a modal dialog box and I'm trying to assign a click event to the button in the form, but I can't seem to...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
1
by: somnamblst | last post by:
This function swaps images between a close & replay GIF. Line 12 has a speed of 1 second specified for the slideDown, but no speed specified for the slideUp so it is defaulting to normal. I tried...
2
by: thj | last post by:
Hi. I've got this form that I'm trying to validate: <form id="periodForm" action="" method="post"> <p> Periode: <input id="startDate" name="startDate" type="text" size="7" value="<%=...
26
by: RobG | last post by:
Do some of the regulars here need to re-think their (sometimes strident) opposition to libraries? Both Microsoft and Nokia have announced support for jQuery. It seems to have gained quite a bit...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
3
by: Reveller | last post by:
I'm rewriting my own js library to jquery functions. A huge undertaking, but hopefully worth it in the future :) Question: why does the script always return 0 for img_width? Logo.gif exists and...
25
pradeepjain
by: pradeepjain | last post by:
<html> <head> <script src="jquery.js" type="text/javascript"></script> <script src="jquery.rating.js" type="text/javascript" language="javascript"></script> <link...
5
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.