473,549 Members | 2,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3497
I mean i need it to validated on this
JSLint - http://www.jslint.com/
Thanks, Daniel
Nov 14 '08 #2
ph***********@g ooglemail.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.aPrope rty = aFunctionObject Reference

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
13991
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 dosent meet these requirments an error message will be displayed. I have pasted the code (and highlighted the relevant parts) below in the hope that...
6
6313
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, column 13: there is no attribute "SRC" <bgsound src="C:\My Documents\zingwent.mids"> You have used the attribute named above in your document, but...
3
1811
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 of javascripts with a body onload " " tag. What I would like is to embedded scripts using xml cdata sections eg. <script type="text/javascript">...
5
2339
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 on-click of a button. My page has 1 dropdown list and two checkboxes. So I either want the user to choose an item from the dropdownlist OR tick a...
7
1592
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 respective controls. If anyone has got any articles or sample pages, please forward.. Thanks..
1
5913
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 if when the list/menu (attribute6) value is "Ja" then to make the TextField Pas Nr (attribute4) required. And if when the list/menu (attribute6)...
16
1946
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 Test(){
24
2964
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 characters"; } it does not accept UNDERSCORE at the beginning or end however while i was testing with different special characters except for # the...
0
7532
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7462
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7730
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7492
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6059
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.