Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect an empty space on a web page

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)

enter image description here

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:

google web page

like image 594
happyOasis Avatar asked Jan 05 '16 12:01

happyOasis


People also ask

How do I remove blank space from a website?

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.

What do you do with whitespace on a website?

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.

What tag is used for space in HTML?

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.

Why is there white space at the top of my website?

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.


1 Answers

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.

like image 84
Roamer-1888 Avatar answered Sep 27 '22 19:09

Roamer-1888