473,320 Members | 2,112 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,320 software developers and data experts.

Validating Javascript function using JLint

hello i am a newbie to Javascript with a in depth background of PHP
i have a simple script that replaces missing images when the webpage
loads
but i just cant get it to validate in JLint as my javascript skills
are not up to scratch yet

please can somebody help me validate this and ensure that it functions
correctly
function fixBrokenImages(){
var imglst = document.images;
for(var i = 0; i < imglst.length; i++){
imglst[i].onerror = function()
{this.src = "/noimage.png";}
imglst[i].src = imglst[i].src;
}
}

Thanks in advanced
Mr Daniel Lee - London UK
Nov 14 '08 #1
2 3485
I mean i need it to validated on this
JSLint - http://www.jslint.com/
Thanks, Daniel
Nov 14 '08 #2
ph***********@googlemail.com wrote:
hello i am a newbie to Javascript with a in depth background of PHP
i have a simple script that replaces missing images when the webpage
loads
but i just cant get it to validate in JLint as my javascript skills
are not up to scratch yet

please can somebody help me validate this and ensure that it functions
correctly
Although your posting style really is atrocious (please read what you wrote
and imagine yourself to be a complete stranger, like someone reading your
job application), I am making an exception here (for reasons that will
become obvious later).
function fixBrokenImages(){
var imglst = document.images;
for(var i = 0; i < imglst.length; i++){
imglst[i].onerror = function()
{this.src = "/noimage.png";}
imglst[i].src = imglst[i].src;
}
}
This cries out for code style (I do hope your so very background-supported
PHP scripts don't look like above either):

function fixBrokenImages()
{
var imglst = document.images;
for (var i = 0; i < imglst.length; i++)
{
imglst[i].onerror = function() {
this.src = "/noimage.png";
}

imglst[i].src = imglst[i].src;
}
}

Now that we can see at a glance what's going on, let's look at what JSLint
said, without any options checked (which you should have posted in the first
place!):

| Implied global: document 2

ISTM you can safely ignore that one. By default, JSLint only checks against
the ECMAScript Language Specification, it does not know that browsers (HTML
UAs in general) provide an API with host objects, like the one that can be
referred to by `document'. If you check the "Assume a browser" option that
error-warning will go away.

| Problem at line 6 character 1: Be careful when making functions within a
| loop. Consider putting the function in a closure.
|
| imglst[i].src = imglst[i].src;

That's more serious, if misleading. The relevant lines are instead
(pretty-printed):

imglst[i].onerror = function() {
this.src = "/noimage.png";
}

That function is created within a loop, through a function expression. If
the function contained the `i' variable of the loop, when called it would
resolve that identifier to the last value `i' was assigned to (probably
imglst.length), not to the value that it had when the function was created.
*Another* closure (a closure is what causes this behavior in the first
place, actually) around it would prevent that, however what is described in
the message is not the case here; `i' is used outside of the function, which
is safe.

We have discussed this here numerous times before, ad nauseam, if I may say
so again.

And finally:

| Problem at line 5 character 29: Missing semicolon.
|
| {this.src = "/noimage.png";}

This is not a problem per se. ECMAScript implementations do automatic
semicolon insertion. So, in contrast to PHP, you do not need to write
trailing semicolons in general to make your code syntactically correct.
However, what is a feature can introduce semantical problems if you do
not know how it works. So it is best to adopt "PHP semicolon style"
here, if you will: always end simple statements, such as assignments,
with a semicolon.

Here the statement is

anObject.aProperty = aFunctionObjectReference

so it should be

imglst[i].onerror = function() {
this.src = "/noimage.png";
};
// ^ semicolon there

(Do not let yourself be deceived by the fact that there is a semicolon in
the function body; that delimits only the "inner" assignment, not the
"outer" one.)

Bottom line is that you should take everything JSLint says with a grain of
salt, and you should use the options to enable more lenient checking.

IMHO, JSLint is _not_ to be considered a validator; due to the differences
in implementations it hardly can be that. What runs in one language
implementation might not run in another, see
<http://PointedEars.de/es-matrix>. So you should always test your code in
all known language implementations (which are pretty much related to the
different HTML UAs that support them) and maybe even different versions thereof.

And what might run in one DOM implementation might also not run in another,
so you should always test your code in all known DOM implementations and
maybe even different versions thereof.

What is marked as an error in JSLint might only be a warning or a matter of
Douglas Crockford's taste; and, unfortunately, even then there appear to be
problems in recognizing the context of an expression.

Now that's clarified, it's time for a little optimization of your code:

function fixBrokenImages()
{
var imglst = document.images;
for (var i = imglst.length; i--;)
{
imglst[i].onerror = function() {
// NOTE: prevents this from going into a "loop"
// if the replacement image is not there
this.onload = this.onerror = null;

this.src = "/noimage.png";
};

// TODO: does not make sense
imglst[i].src = imglst[i].src;
}
}

HTH
P.S.: When you reply or make a new thread here, please adhere to
<http://jibbering.com/faq/#postingpp.
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Nov 15 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark | last post by:
Hi, Im trying to validate a form, all the validating works apart from one field. This particular field must consist of the first 2 characters as letters, & the following 5 as numbers. And if it...
6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
3
by: jon | last post by:
Hello, I am new to trying to validate this xhtml so I will try to explain as best I can. Ok, I currently have a draft html file which I am trying to validate to a xhtml. Right now I have a couple...
5
by: Stephen | last post by:
Could someone please help me with some validation. I have to write code which checks to see whether a dropdown list is populated with a value or a checkbox is checked. I want the code to run on the...
7
by: ani | last post by:
I have two submit buttons in my form which need to validate two different controls on the page . How do I validate portions of the asp.net page. The two submit buttons should validate their...
1
by: platostoteles | last post by:
Hallo NG, I am new to JavaScript and would really appreciate any help to solve my problem. I am using the blow code in my form to validate form fields. What I would like to accomplish is that...
16
by: shyamg | last post by:
Hi, this is my javascript validating the fields in mozilla FF but its working and validating only one field. how to write the and how to works the script .................. function...
24
by: runway27 | last post by:
i have used the following code to validate the username it is working fineif( $username == "" || !preg_match("/^+(?:_+)?$/i", $username) ) { $error.="User name cannot be blank or has special...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.