- Открытие нового окна window.open() или одной страницы внутри другой с помощью iframe | JavaScript
- Правильный скрипт popup, сделанный с помощью iframe
- Изменить содержимое iframe
- How to include another HTML file in an HTML file?
- Using JQuery load to include an HTML file
- Syntax
- Parameters
- Example
- Inner HTML file
- Main HTML file
- Output
- Using w3-include-html attribute to include an HTML file
- Syntax
- Example
- Inner HTML file
- Main HTML file
- Output
- Using the htmlinclude library to include an HTML file
- Syntax
- Example
- Inner HTML file
- Main HTML file
- Output
- Using an iframe to include an HTML file
- Syntax
- Example
- Inner HTML file
- Main HTML file
- Output
- How TO — CSS/JS Modal
- How To Create a Modal Box
- Modal Header
- Example
- Example
- Example
- Add Header and Footer
- Example
- Modal Header
- Modal Footer
- Example
- Bottom Modal («Bottom sheets»)
- Example
Открытие нового окна window.open() или одной страницы внутри другой с помощью iframe | JavaScript
Правильный скрипт popup, сделанный с помощью iframe
Считаю, что этот вариант более удачный по сравнению с модальным окном только на CSS, так как дополнительный HTML подгружается после нажатия на кнопку или другого действия посетителя сайта Страница родителя Вложенная страница, вернее страница, которая будет открыта внутри iframewindow.parent
даёт доступ к ближайшему родительскому окну из iframe или object, позволяет изменять его элементы, если домены одинаковыwindow.top
даёт доступ к верхнему iframe в иерархии объектов, позволяет изменять его элементы, если домены одинаковы
Изменить содержимое iframe
iframe.contentWindow
позволяет изменять содержимое фрейма, если домены одинаковы. В том числе прокручивать к нужному месту документ в фреймеpostMessage
позволяет делать тоже самое и с разными доменами (есть пример использования)
How to include another HTML file in an HTML file?
In this tutorial, we shall learn to include another HTML file in an HTML file.
Various methods are available to include another HTML file in an HTML file. Let us quickly go through the techniques that have support on the web.
Using JQuery load to include an HTML file
In this section, we shall check how to use JQuery’s load method to include an HTML file.
Users can follow the syntax below to use this method.
Syntax
The wrapper appends the new HTML file that jQuery loads.
Parameters
- wrapper − ID of the DOM element that includes the new HTML content.
- htmlfile − The new HTML file name.
Example
The program requires two HTML files. One is the main HTML file to load the new HTML file. Next is the new HTML file. Place both files in the exact location.
Define a wrapper DOM element in the main HTML file to append the new HTML file. Using the jQuery load technique load the new HTML file and set it inside the wrapper DOM.
Inner HTML file
html> body> h3>This is an HTML page from same directoryh3> body> html>
Main HTML file
html> head> script src="https://code.jquery.com/jquery-1.11.3.min.js">script> script> $(function() < $("#includeHtml").load("result.html"); >); script> head> body> h2>Program to include another HTML file in this HTML using i>JQuery loadi>h2> div id="includeHtml">div> body> html>
Output
Using w3-include-html attribute to include an HTML file
In this section, let us check how to use the w3-include-html attribute to include an HTML file.
Users can follow the syntax below to use this method.
Syntax
Includes a wrapper DOM with the attribute w3-include-html having the new HTML file name as the value.
//Read the attribute fileName = domEl.getAttribute("w3-include-html"); //Make XMLHttpRequest with the attribute value xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() < //If the request is successful, load the new HTML. Else throw 404 error and try again if (this.readyState == 4) < if (this.status == 200) if (this.status == 404) /* Remove the attribute and invoke the function again*/ domEl.removeAttribute("w3-include-html"); addHTML(); > xmlHttp.open("GET", fileName, true); xmlHttp.send();
The syntax reads the w3-include-html attribute value and loads the new HTML with a XMLHttpRequest.
Example
In this example, create a new HTML and a default HTML file. The default HTML file contains a div element with attribute w3-include-html which contains the new HTML file name.
The program reads the w3-include-html attribute value, makes an XMLHttpRequest with the file name, and loads the file.
A New HTML file renders inside the w3-include-html wrapper DOM after a successful file load. Else user gets an error message, and the program loads the file again.
Inner HTML file
html> body> header>b>HTML 2 HEADERb>header> div style="height: 100px;">div> footer>b>HTML 2 FOOTERb>footer> body> html>
Main HTML file
html> body> h2>Program to include another HTML file in this HTML using i>w3-include-htmli>h2> div w3-include-html="result.html">div> script> function addHTML() < var el, i, domEl, fileName, xmlHttp; /*Iterate all DOM*/ el = document.getElementsByTagName("*"); for (i = 0; i < el.length; i++) < domEl = el[i]; /*find the element having w3-include-html attribute*/ fileName = domEl.getAttribute("w3-include-html"); if (fileName) < /*http request with attribute value as file name*/ xmlHttp = new XMLHttpRequest(); xmlHttp.onreadystatechange = function() < if (this.readyState == 4) < if (this.status == 200) < domEl.innerHTML = this.responseText; >if (this.status == 404) < domEl.innerHTML = "Page not found."; >/* Remove the attribute and invoke the function again*/ domEl.removeAttribute("w3-include-html"); addHTML(); > > xmlHttp.open("GET", fileName, true); xmlHttp.send(); /*function ends*/ return; > > > addHTML(); script> body> html>
Output
Using the htmlinclude library to include an HTML file
In this section, we shall check how to use the htmlinclude library to include an HTML file.
Users can follow the syntax below to use this method.
Syntax
The syntax adds the javascript library file URL.
The include tag src contains the new HTML file name.
//Getting include attribute value let includes = document.getElementsByTagName('include'); for(var i=0; i); > function load_file(filename, callback) < fetch(filename).then(response =>response.text()).then(text => callback(text)); >
The syntax loads the source of the tag «include» using the fetch method.
Example
In this example, the htmlinclude library is available in the header. Creating an include tag with the new file name as the src attribute value.
Coming to the script, loading the include tag src value with the fetch method. If you get any error using fetch, try to get help from nodejs or any other program IDE.
Inner HTML file
html> body> b>htmlinclude library included this HTML fileb> body> html>
Main HTML file
html> head> script src="https://unpkg.com/htmlincludejs">script> head> body> h2>Program to include another HTML file in this HTML using i>htmlinclude libraryi>h2> include src="./result.html">include> script> let includes = document.getElementsByTagName('include'); for (var i = 0; i < includes.length; i++) < let include = includes[i]; load_file(includes[i].attributes.src.value, function(text) < include.insertAdjacentHTML('afterend', text); include.remove(); >); > function load_file(filename, callback) < fetch(filename).then(response =>response.text()).then(text => callback(text)); > script> body> html>
Output
Using an iframe to include an HTML file
In this section, let us check how to use an iframe to include an HTML file.
Users can follow the syntax below to use this method.
Syntax
The iframe tag includes the new HTML file name in the src.
Example
In this example, create a sample HTML file to include and main HTML file. The approach adds an iframe with the new HTML file name as the source value in the new HTML body.
The iframe loads the new HTML content inside the main HTML file.
Inner HTML file
html> body> div style="background-color: skyblue;">iframe included this HTML filediv> body> html>
Main HTML file
html> head> h2>Program to include another HTML file in this HTML using i>iframei>h2> style> iframe[iframetag] < border: none; >style> head> body> div id="iframeDiv"> iframe src="result.html" iframetag>iframe> div> body> html>
Output
This tutorial introduced four methods to include a new HTML file in an HTML file. The iframe method is simple to implement. Users can choose the jQuery load method if they need a jQuery method.
How TO — CSS/JS Modal
Learn how to create a Modal Box with CSS and JavaScript.
How To Create a Modal Box
A modal is a dialog box/popup window that is displayed on top of the current page:
Modal Header
Step 1) Add HTML:
Example
class=»modal» is a container element for the modal and the div with class=»modal-content» is where you put your modal content (headings, paragraphs, images, etc).
The element with class=»close» should be used to close the modal.
Step 2) Add CSS:
Example
/* The Modal (background) */
.modal display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
>
/* Modal Content/Box */
.modal-content background-color: #fefefe;
margin: 15% auto; /* 15% from the top and centered */
padding: 20px;
border: 1px solid #888;
width: 80%; /* Could be more or less, depending on screen size */
>
/* The Close Button */
.close color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
>
.close:hover,
.close:focus color: black;
text-decoration: none;
cursor: pointer;
>
The .modal class represents the window BEHIND the actual modal box. The height and width is set to 100%, which should create the illusion of a background window.
Add a black background color with opacity.
Set position to fixed; meaning it will move up and down the page when the user scrolls.
It is hidden by default, and should be shown with a click of a button (we’ll cover this later).
The .modal-content class
This is the actual modal box that gets focus. Do whatever you want with it. We have got you started with a border, some padding, and a background color. The margin: 15% auto is used to push the modal box down from the top (15%) and centering it (auto).
We also set the width to 400px — this could be more or less, depending on screen size. We will cover this later.
The .close class
The close button is styled with a large font-size, a specific color and floats to the right. We have also added some styles that will change the color of the close button when the user moves the mouse over it.
Step 3) Add JavaScript:
Example
// Get the modal
var modal = document.getElementById(«myModal»);
// Get the button that opens the modal
var btn = document.getElementById(«myBtn»);
// Get the element that closes the modal
var span = document.getElementsByClassName(«close»)[0];
// When the user clicks on the button, open the modal
btn.onclick = function() modal.style.display = «block»;
>
// When the user clicks on (x), close the modal
span.onclick = function() modal.style.display = «none»;
>
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) if (event.target == modal) modal.style.display = «none»;
>
>
Add Header and Footer
Add a class for modal-header, modal-body and modal-footer:
Example
Modal Header
Some text in the Modal Body
Some other text.
Modal Footer
!-->
Style the modal header, body and footer, and add animation (slide in the modal):
Example
/* Modal Header */
.modal-header padding: 2px 16px;
background-color: #5cb85c;
color: white;
>
/* Modal Footer */
.modal-footer padding: 2px 16px;
background-color: #5cb85c;
color: white;
>
/* Modal Content */
.modal-content position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 1px solid #888;
width: 80%;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
animation-name: animatetop;
animation-duration: 0.4s
>
Bottom Modal («Bottom sheets»)
An example on how to create a full-width modal that slides in from the bottom:
Example
Tip: Also check out Modal Images and Lightbox.