


Use jquery's load method to design dynamic loading and solve the problem of JavaScript failure on the loaded page
1. Problem Analysis
Everyone has an impression of the backend system and knows its layout structure, as shown in the figure:
In In this layout, we need to separate the header, sidebar, and footer, and the content in the middle part needs to be dynamically changed, that is, different pages are positioned according to different menus, but the overall layout will not change
This layout structure Pure HTML does not have the ability to embed various parts of content, so we need to find or solve this problem ourselves. Since jquery has good compatibility and breadth of use, here
uses jquery's load Method to handle this Page Layout framework.
2. Detailed explanation of load method
1.定义 $().load(,,);
The required URL parameter specifies the URL you want to load.
The optional data parameter specifies the set of query string key/value pairs sent with the request.
The optional callback parameter is the name of the function executed after the load() method is completed.
2. Example
You can also add jQuery selectors to URL parameters.
The following example loads the content of the element with id="p1" in the "demo_test.txt" file into the specified
element:
$("#p1").load("demo_test.txt #p1");
The optional callback parameter specifies the callback function to be allowed when the load() method completes. The callback function can be set to different
$("#p1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){ if(statusTxt=="success") alert("外部内容加载成功!"); if(statusTxt=="error") alert("Error: "+xhr.status+": "+xhr.statusText); });
3. Use of layout framework load
1. Problem
A common problem that many people on the Internet encounter when using the load method to load dynamic pages is that the JavaScript code in the loaded page is invalid. This is because the external file loaded by load will delete the Script part. , so when the JavaScript of the page is called in the loaded page, xxxfunction will be undefined.
2. Solution
For header, sidebar, footer, which only contain static HTML code, use load directly to load
The content corresponding to the intermediate content change generally contains the corresponding JavaScript code. Use the custom load method (the following code). While using the jquery.load() method to load the corresponding content, use the load callback method to process the JavaScript. Load, load the JavaScript code of the loaded page into the
of the layout page. In this way, the content of the content will be overwritten every time load() is executed, so there is no need to worry. Repeated loading issue. This perfectly solves the problem of js failure on the loaded page. The specific code is as follows:
4. Code example
Layout page:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title></title> <!-- Tell the browser to be responsive to screen width --> <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport"> <!-- Bootstrap 3.3.6 --> <link rel="stylesheet" href="../resources/bootstrap/css/bootstrap.min.css"> </head> <body class="hold-transition skin-blue-light sidebar-mini" onload="onload();"> <p class="wrapper"> <p id="header"> </p> <!-- Left side column. contains the logo and sidebar --> <p id="sidebar"> </p> <!-- Content Wrapper. Contains page content --> <p id="content" class="content-wrapper clearfix"> <!-- Content Header (Page header) --> </p> <!-- /.content-wrapper --> <p id="footer"> </p> <!-- Add the sidebar's background. This p must be placed immediately after the control sidebar --> <p class="control-sidebar-bg"></p> </p> <!-- ./wrapper --> <!-- jQuery 2.2.3 --> <script src="../resources/plugins/jQuery/jquery-2.2.3.min.js"></script> <!-- Bootstrap 3.3.6 --> <script src="../resources/bootstrap/js/bootstrap.min.js"></script> <!--左侧菜单--> <script src="../resources/dist/js/common/global.js"></script> <script src="../resources/dist/js/menu/menuTemplate.js"></script> <script src="../resources/dist/js/menu/menu.js"></script> </body> <script> //加载页面布局的header,sidebar,footer的内容 $("#header").load("inc/header.html"); $("#sidebar").load("inc/sidebar.html"); $("#footer").load("inc/footer.html"); /* *加载变换内容,主要url参数为dom对象,并且该dom中的url放在href中, *调用方式如:<span onclick="javascript:load(this);" href="/backstage/website/test.html">测试</span> *注意:1.该dom对象最好不要用a标签,因为点击a标签会进入href指定的页面 * 2.要加载的内容要用 id="content" 标注,因为load中指明了加载页面中指定的id为content下的内容 * 3.对应页面的JavaScript写在content下 */ function load(url, data){ //alert($(url).attr("href")); $.ajaxSetup({cache: false }); $("#content").load($(url).attr("href")+ " #content ", data, function(result){ //alert(result); //将被加载页的JavaScript加载到本页执行 $result = $(result); $result.find("script").appendTo('#content'); }); }</script> </html>
Loaded page:
<p id="content"> <p>测试二</p> <span onclick="javascript:load(this);" href="/backstage/website/test.html">测试</span> <a href="javascript:test();">测试</a> <script> function test(){ alert("测试二页面"); } </script> <script> function test2(){ alert("ceshi"); } </script> </p>
Effect screenshot:
The above is the detailed content of Use jquery's load method to design dynamic loading and solve the problem of JavaScript failure on the loaded page. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

Detailed explanation of jQuery reference method: Quick start guide jQuery is a popular JavaScript library that is widely used in website development. It simplifies JavaScript programming and provides developers with rich functions and features. This article will introduce jQuery's reference method in detail and provide specific code examples to help readers get started quickly. Introducing jQuery First, we need to introduce the jQuery library into the HTML file. It can be introduced through a CDN link or downloaded

How to use PUT request method in jQuery? In jQuery, the method of sending a PUT request is similar to sending other types of requests, but you need to pay attention to some details and parameter settings. PUT requests are typically used to update resources, such as updating data in a database or updating files on the server. The following is a specific code example using the PUT request method in jQuery. First, make sure you include the jQuery library file, then you can send a PUT request via: $.ajax({u

How to remove the height attribute of an element with jQuery? In front-end development, we often encounter the need to manipulate the height attributes of elements. Sometimes, we may need to dynamically change the height of an element, and sometimes we need to remove the height attribute of an element. This article will introduce how to use jQuery to remove the height attribute of an element and provide specific code examples. Before using jQuery to operate the height attribute, we first need to understand the height attribute in CSS. The height attribute is used to set the height of an element

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: <

The relationship between js and vue: 1. JS as the cornerstone of Web development; 2. The rise of Vue.js as a front-end framework; 3. The complementary relationship between JS and Vue; 4. The practical application of JS and Vue.

Title: Use jQuery to modify the text content of all a tags. jQuery is a popular JavaScript library that is widely used to handle DOM operations. In web development, we often encounter the need to modify the text content of the link tag (a tag) on the page. This article will explain how to use jQuery to achieve this goal, and provide specific code examples. First, we need to introduce the jQuery library into the page. Add the following code in the HTML file:

jQuery is a popular JavaScript library that is widely used to handle DOM manipulation and event handling in web pages. In jQuery, the eq() method is used to select elements at a specified index position. The specific usage and application scenarios are as follows. In jQuery, the eq() method selects the element at a specified index position. Index positions start counting from 0, i.e. the index of the first element is 0, the index of the second element is 1, and so on. The syntax of the eq() method is as follows: $("s
