Why does a social site like Reddit use this method?

See what Reddit uses to add one of its buttons:

<script type="text/javascript" src="http://www.reddit.com/button.js?t=2"></script> 

This JavaScript adds an <iframe> to the page, then <iframe> adds the HTML code.

Why doesn't JavaScript add HTML directly?

+4
source share
2 answers

To isolate button layout and style using custom CSS rules.

+4
source

This method is called an unobtrusive JavaScript link. This is one of the good practices for developing a web page with graceful degradation. Actual HTML does not contain JavaScript references, and JavaScript should not cause manipulation of the content.

Another reason JavaScript is included at the end of the file is because the web page may display without waiting for the JavaScript to fully load. This is the exact complement to why CSS files are included at the beginning (to prevent content from being shown before styles are set).

+2
source

Source: https://habr.com/ru/post/1301188/


All Articles