Use the same HTML snippet code on multiple pages

Source: Internet
Author: User

Problem description
There is A complicated HTML clip (A). If you embed this HTML clip into other pages (B, C, D ....).
The key issue is that a large number of JavaScript logic needs to be processed in HTML fragments, such as paging and click event response.

We use a simple example to illustrate this problem:
"There is a button on the page. Click this button to introduce an HTML clip, which contains a paging button ."

1. Use IFrame
On the homepage, click a button to introduce an IFrame to the page:

Copy codeThe Code is as follows: <script type = "text/javascript">
$ (Function (){
$ ("# ClickToInsert"). click (function (){
$ ("# Placeholder" pai.html ('<iframe src = "iframe.htm"> </iframe> ');
});
});
</Script>
<Input type = "button" id = "clickToInsert" value = "Insert HTML"/>
<Div id = "placeholder">
</Div>
IFrame page to simulate paging:
<Script type = "text/javascript">
$ (Function (){
Var parent = $ ("# complex_page_segment ");
$ (". Previous", parent). click (function (){
$ (". Content", parent).html ("Previous Page Content ");
});
$ (". Next", parent). click (function (){
$ (". Content", parent).html ("Next Page Content ");
});
});
</Script>
<Div id = "complex_page_segment">
<Input type = "button" value = "Previous Page" class = "previous"/>
<Input type = "button" value = "Next Page" class = "next"/>
<Div class = "content">
Page Content </div>
</Div>

2. AJAX returns the page snippet and registers the event
Note: We use textarea to simulate returned HTML fragments.

Copy codeThe Code is as follows: <script type = "text/javascript">
$ (Function (){
$ ("# ClickToInsert"). click (function (){
$ ("# Placeholder" cmd.html ($ ("# clone"). val ());
Var parent = $ ("# complex_page_segment ");
$ (". Previous", parent). click (function (){
$ (". Content", parent).html ("Previous Page Content ");
});
$ (". Next", parent). click (function (){
$ (". Content", parent).html ("Next Page Content ");
});
});
});
</Script>
<Input type = "button" id = "clickToInsert" value = "Insert HTML"/>
<Div id = "placeholder">
</Div>
<Textarea id = "clone" style = "display: none;">
<Div id = "complex_page_segment">
<Input type = "button" value = "Previous Page" class = "previous"/>
<Input type = "button" value = "Next Page" class = "next"/>
<Div class = "content"> Page Content </div>
</Div>
</Textarea>

Because we need to reference the same HTML segment on multiple pages, this method causes a large number of processing tasks to be copied and pasted repeatedly. Obviously, we need to extract common methods.

3. AJAX returns the page fragment and calls the function registration event in the page fragment.

Copy codeThe Code is as follows: <script type = "text/javascript">
$ (Function (){
$ ("# ClickToInsert"). click (function (){
$ ("# Placeholder" cmd.html ($ ("# clone"). val ());
Init_complex_page_segment ();
});
});
</Script>
<Input type = "button" id = "clickToInsert" value = "Insert HTML"/>
<Div id = "placeholder">
</Div>
<Textarea id = "clone" style = "display: none;">
<Script type = "text/javascript">
Function init_complex_page_segment (){
Var parent = $ ("# complex_page_segment ");
$ (". Previous", parent). click (function (){
$ (". Content", parent).html ("Previous Page Content ");
});
$ (". Next", parent). click (function (){
$ (". Content", parent).html ("Next Page Content ");
});
}
</Script>
<Div id = "complex_page_segment">
<Input type = "button" value = "Previous Page" class = "previous"/>
<Input type = "button" value = "Next Page" class = "next"/>
<Div class = "content"> Page Content </div>
</Div>
</Textarea>

In fact, we can go further. There is no need to manually call this function, but we can automatically execute it in the returned HTML snippet.

4. AJAX returns a page segment and its event is automatically registered

Copy codeThe Code is as follows: <script type = "text/javascript">
$ (Function (){
$ ("# ClickToInsert"). click (function (){
$ ("# Placeholder" cmd.html ($ ("# clone"). val ());
});
});
</Script>
<Input type = "button" id = "clickToInsert" value = "Insert HTML"/>
<Div id = "placeholder">
</Div>
<Textarea id = "clone" style = "display: none;">
<Script type = "text/javascript">
$ (Function (){
Var parent = $ ("# complex_page_segment ");
$ (". Previous", parent). click (function (){
$ (". Content", parent).html ("Previous Page Content ");
});
$ (". Next", parent). click (function (){
$ (". Content", parent).html ("Next Page Content ");
});
});
</Script>
<Div id = "complex_page_segment">
<Input type = "button" value = "Previous Page" class = "previous"/>
<Input type = "button" value = "Next Page" class = "next"/>
<Div class = "content"> Page Content </div>
</Div>
</Textarea>

The last method and the first IFrame method are recommended.
Download source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.