Is there a way to detect an empty area, without text or images within a web page, using JavaScript?
More precisely, how to determine whether point [x,y] is within a blank area, like in the following example (marked in red)
EDIT: I want to make my question clearer, I'm building an extension which supposed to mark search results as trustworthy or as spam, I want to put my marking at the end of the text of a result item URL. I also want to do it in a generic way, so it wouldn't work only in Google web page. an example is shown below:
Approach 1: We can remove space between any two tags by simply using margin-bottom property. The margin-bottom property sets the bottom margin of an element. We will assign a negative value to the margin-bottom of a particular tag to eliminate the extra space between the tags as shown.
Whitespace (also called negative space) is the area between the elements on a web page (or physical page). These elements typically are images, typography, and icons. It is often used to balance elements on a page by creating a natural flow for the user to navigate through the content.
To add real spaces to your text, you can use the character entity. Tip: The non-breaking hyphen (‑) is used to define a hyphen character (‑) that does not break into a new line.
If you're using a layout that includes your theme's styling, it's possible that you see some space or a 'gap' between your header and where your content starts, or between your content and the footer of your page. This space is actually a part of your theme's layout.
You can test for genuine white space like this :
function isWhiteSpace(coords) {
var element = document.elementFromPoint(coords.x, coords.y);
var whitespace = $(document).add("body, html");
return (whitespace.get().indexOf(element) > -1) ? true : false;
}
where coords
is an object with .x
and .y
properties.
DEMO
document.elementFromPoint()
, documented here, is "an experimental technology", so I wouldn't trust my life to it. Test thoroughly on all target platforms.
Edit
For the full detection of all the white you seek, isWhiteSpace()
would be the first of two stages. The second stage would be isVisualWhiteSpace()
implemented with @remdevtec's approach for example.
As my isWhiteSpace(coords)
is inexpensive, you would perform it first and only if it returned false go for the expensive test. You could use the protective property of ||
var isWhite = isWhiteSpace(coords) || isVisualWhiteSpace(coords);
But your real problem will be writing isVisualWhiteSpace()
. I can't help with that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With