link
tag with import
'in the parameter of rel
and href
, containing the path to the desired file. For example, if you want to load a file called component.html
into index.html
, then everything should look like this:index.html
<link rel="import" href="component.html" >
component.html
<link rel="stylesheet" href="css/style.css"> <script src="js/script.js"></script>
doctype
, html
, head
, body
optional. HTML imports will automagically load all the specified elements, add them to the page and run JavaScript, if any.script
at the beginning of HTML will be loaded earlier than the same, but at the end. Please note that some browsers are waiting for the script to complete before loading the following items.script
tag, you can use async
/ defer
(you can also move all scripts to the bottom of the page). defer
indicates that the code can be run only after loading HTML. async
allows the browser to perform these two actions in parallel.script
tag with the defer
attribute. In the example below, index.html
will run script1.js
and script2.js
inside component.html
before executing script3.js
.index.html
<link rel="import" href="component.html"> // 1. <title> </title> <script src="script3.js"></script> // 4.
component.html
<script src="js/script1.js"></script> // 2. <script src="js/script2.js"></script> // 3.
component.html
loaded from index.html
and is awaiting execution;script1.js
into component.html
;script2.js
into component.html
after script1.js
;script3.js
into index.html
after script2.js
.async
attribute to the link[rel="import"]
regarded as an async
attribute in the script
tag . It will not block HTML. This can potentially improve the performance of your project.document
objects in imported filesdocument
object in the imported file will link to the original page.document
in index.html
and component.html
reference the document
in index.html
.index.html
var link = document.querySelector('link[rel="import"]'); link.addEventListener('load', function(e) { var importedDoc = link.import; // importedDoc points to the document under component.html });
document
from component.html
add your code with document.currentScript.ownerDocument
.component.html
var mainDoc = document.currentScript.ownerDocument; // mainDoc points to the document under component.html
webcomponents.js
, use document._currentScript
instead of document.currentScript
. The underscore in currentScript
used to support browsers that cannot work with this component without using this sign.component.html
var mainDoc = document._currentScript.ownerDocument; // mainDoc points to the document under component.html
document
from component.html
, even if the browser does not support HTML imports. document._currentScript = document._currentScript || document.currentScript;
index.html
<link rel="import" href="component1.html"> <link rel="import" href="component2.html">
component1.html
<script src="js/jquery.js"></script>
component2.html
<script src="js/jquery.js"></script>
script
tag, the objects of our article do not reload the same material. Looking at the last example, it is enough to wrap the script
HTML HTML tag with import, and this resource will not be loaded twice.npm
and use the command line. There are also analogues for grunt
and gulp
, with which you can make “Vulcanize” part of your build process.index.html
files, use the following code: $ vulcanize -o vulcanized.html index.html
index.html
will be connected in the file vulcanized.html
.styles
, ID
and classes
element can be used a little differently. With Custom Elements you can create your own HTML tags. Not bad, right?link
tag.x-component.html
<template id="template"> <style> ... </style> <div id="container"> <img class="webcomponents" src="http://webcomponents.org/img/logo.svg"> <content select="h1"></content> </div> </template> <script> // This element will be registered to index.html // Because `document` here means the one in index.html var XComponent = document.registerElement('x-component', { prototype: Object.create(HTMLElement.prototype, { createdCallback: { value: function() { var root = this.createShadowRoot(); var template = document.querySelector('#template'); var clone = document.importNode(template.content, true); root.appendChild(clone); } } }) }); </script>
index.html
... <link rel="import" href="x-component.html"> </head> <body> <x-component> <h1>This is Custom Element</h1> </x-component> ...
document
object in x-component.html
is the same as in index.html
. There is no need to write anything difficult, everything works on its own Source: https://habr.com/ru/post/241135/
All Articles