Tuesday, September 27, 2016

Are all html element closed? Testing it with Javascript

This small snippet check if all html tags are properly closed.

The html can be passed as a string or as an array containing html strings. It is based on the work of Jonathan Aquino that has an online form that has a similar script implemented: http://jona.ca/blog/unclosed-tag-finder

The script run both in NodeJS and in the browser console. I added also a function that allow to test the html coming from a server using the URL.

The usage is:

凸.checkHtml("your html here")

or

凸.checkHtml(["your html here", "and here"])

If you want to check a page served by a server:

凸.buildGetPageFunction()('http://example.com', function(data){console.log(凸.checkHtml(data))})

These commands are available in the console after you copy and paste the snipped there.

The code can run also server side with

$ node closed-tag-testing.js

Be aware that in the console you can only check pages that belong to the same domain where you are in. If you try

凸.buildGetPageFunction()('http://google.com', function(data){console.log(凸.checkHtml(data))})

While being in http://example.com you will get the error:

XMLHttpRequest cannot load http://google.com/. It has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.


No comments :

Post a Comment