Issues (105)

reports/jquery.isonscreen.js (5 issues)

1
/* Copyright (c) 2010
2
 * @author Laurence Wheway
3
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
4
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
5
 *
6
 * @version 1.2.0
7
 */
8
(function($) {
9
	jQuery.extend({
10
		isOnScreen: function(box, container) {
11
			//ensure numbers come in as intgers (not strings) and remove 'px' is it's there
12
			for(var i in box){box[i] = parseFloat(box[i])};
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
13
			for(var i in container){container[i] = parseFloat(container[i])};
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable i already seems to be declared on line 12. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
14
15
			if(!container){
16
				container = {
17
					left: $(window).scrollLeft(),
18
					top: $(window).scrollTop(),
19
					width: $(window).width(),
20
					height: $(window).height()
21
				}
22
			}
23
24
			if(	box.left+box.width-container.left > 0 &&
25
				box.left < container.width+container.left &&
26
				box.top+box.height-container.top > 0 &&
27
				box.top < container.height+container.top
28
			) return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
			return false;
30
		}
31
	})
32
33
34
	jQuery.fn.isOnScreen = function (container) {
35
		for(var i in container){container[i] = parseFloat(container[i])};
0 ignored issues
show
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
36
37
		if(!container){
38
			container = {
39
				left: $(window).scrollLeft(),
40
				top: $(window).scrollTop(),
41
				width: $(window).width(),
42
				height: $(window).height()
43
			}
44
		}
45
46
		if(	$(this).offset().left+$(this).width()-container.left > 0 &&
47
			$(this).offset().left < container.width+container.left &&
48
			$(this).offset().top+$(this).height()-container.top > 0 &&
49
			$(this).offset().top < container.height+container.top
50
		) return true;
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
51
		return false;
52
	}
53
})(jQuery);
54