Home  >  Article  >  Web Front-end  >  How to introduce external pages in Html

How to introduce external pages in Html

小云云
小云云Original
2018-03-29 16:31:093028browse

Usually the head, foot, rightBar (entries on the right) of a website are the same. This part of the content can be introduced. Otherwise, once it needs to be modified, ten, twenty, or even more will need to be modified. page is a tedious but meaningless task. With the help of PHP or JSP, this problem can be easily solved using include, but if we break away from the back-end language, can this problem be solved from the front-end perspective?

1. With the help of iframe

First of all, the easiest thing to think of is to use iframe. Although html5 has abolished frame, it still retains iframe. We can still continue to use iframe. There is a frameboder attribute. Set the attribute value to 0 or no to remove the iframe border. Then set scrolling to no. This is completely doable, but remember to run it in a server environment.

 var frame = document.getElementsByTageName("iframe")[0];
    frame.contentWindow.document.XXX方法,
    如frame.contentWindow.document.querySelector("#btn");//获取iframe中Id为btn的节点.

Because I have no experience using iframe to introduce the header before, considering that the header usually has another function besides jumping, it should be positioning. When the page is long, click to accurately locate a certain point. at. The jump of the page has no effect if introduced using iframe, but what about the anchor point? You need to try this to know.
Here, add some knowledge about anchor points:
Anchor points can jump to the corresponding position of the current page, and can also jump to the corresponding position of other pages.
There are two ways to implement anchor points, one is a tag + name attribute, and the other is to use the Id attribute of the tag.
The details are as follows:
a. Use the a tag + name attribute

    详情 
    

Click "Details" and jump to d39f3d78f2b78439de57b855cdb22445 The location.

b. Use the id attribute of the tag

    详情
    

Click "Details" to jump to the location where eb6e57d964924c85bfd31f6428bb2a1d.

The anchor point often fails when using a+name, so it is recommended to use id to bind the anchor point.
Getting back to the subject, after introducing iframe, can we locate the corresponding position by clicking on the element in the iframe? Here, we use iframe to introduce head.html, which is also my original purpose.
So what we want to achieve is: click on the a tag of the iframe and locate the corresponding position of the main Html. Through implementation, we found that it cannot be achieved simply through html, but it can be done with the help of JS.


    
    
        
        
        
        
        Document
        
        
    

detail

There are elements with IDs bot and top in the iframe. Positioning is achieved through JS.
In the main page, the document in the iframe can be returned as an HTML object through iframe.contentWindow, and the returned object can be processed through all standard DOM methods.
In the iframe page, locate the parent html through parent, and you can locate the top-level html through top.
When calling between iframes at the same level, you need to locate the parent html first, and then locate the iframe.
Supplementary Let’s learn about the anchor point. Its key function is the #detail added after the connection address (detail only refers to it in general). If the current url is localhost:8080/index.html. Then after the anchor point, the url should be localhost: 8080/index.html#detail
The URL address has a "#" identifier at the end, indicating that it needs to jump to the corresponding location. #idName, the browser will find a tag that matches the characteristics of "#idName" on the page. If the characters following "#" in the URL cannot be found in the text, if it is the current page, then it will not jump. If it jumps from another page, the top of the page will be displayed.
Return to the top of the page. In addition to setting the scrollTop of the body through JS (0 returns to the top, sets it to the height of the body, and jumps to the top), another method is fdb1f4b5d831a8c25a3f2f6fa4c446f8Back to top5db79b134e9f6b82c0b36e0489ee08ed

2. With the help of ajax (jquery’s load method)

There is another method, which is to use the jQuery’s load method to load Enter the page.
load(url, data, callback); url is the URL of the HTML webpage to be loaded; data: key/value sent to the server; callback: callback function when loading is successful.

 $(function(){
        $("selector1").load("page1.html");
        $("selector2").load("page2.html");
        $("selector3").load("page3.html");
    });

The DOM structure added through js has an impact on SEO (search engine optimization), and Baidu spiders cannot crawl it! Under normal circumstances, it is not recommended to use it except as a last resort. page1.html/page2.html/page3.html just write the required Html fragment, because it is loaded in, that is, asynchronous loading. When you need to obtain the elements of pages such as page1.html, you can use it in conjunction with setTimeout to ensure that the page is loaded Load it in.

3、使用HTML imports

HTML imports提供了一种在一个HTML文档中包含和重用另一个HTML文档的方法。目前谷歌已经全面支持HTML imports,Opera35版本之后支持,但是FF依旧不支持。(在谷歌的地址栏输入:chrome://flags,启动或禁止一些功能) 
尽管目前HTML imports的兼容不是很好,但是我们还是有必要了解其使用方法,W3C已经发布了HTML imports的标准草案,相信后期应该还是会用得比较普遍的。使用HTML imports 


    
    
        
        
        
               
        Document
        
    

给出了两种将import进来的html中我们需要的部分插入到当前html.

最后简单介绍document.querySelector和document.querySelectorAll,这两个方法是HTML5在Web API中新引入的方法,大大简化了在原生Javascript代码中选取元素。 
document.querySelector和document.querySelectorAll都是接收一个字符串作为参数,这个参数需要符合CSS选择语法,即:标签、类选择器、ID选择器,属性选择器(E[type=”XX”]),结构选择器(:nth-child(n))等。不支持伪类选择器。 
document.importNode(node,deep)方法把一个节点从另一个文档复制到该文档以便应用,第二个值为true,那么将该节点的所有子孙节点也复制过来。 
node.cloneNode(deep):对已有的节点进行克隆,deep值为true,表示克隆其子孙节点。如果deep为false,则只克隆该节点自身。

除了以上方法外,目前更为主流的一种方式是使用组件化开发。每一部分作为一个组件。

The above is the detailed content of How to introduce external pages in Html. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn