Jquery. artdialog is estimated to not support amd specifications. Therefore, when using the modular methods of requirejs and AMD, there is no problem at the beginning. Later, in the slow network environment, it was found that jquery was not fully loaded. artdialog has been loaded and an error is reported, causing you to refresh the page.
The solution provided by requirejs is called Shim, see: http://requirejs.org/docs/api.html#config-shim
I tried it multiple times and figured out how to do it:
1. a js file will be loaded on each HTML page, and require. config will be called in this JS file. All the configurations will be written in it, including Shim
For example, the login.html page contains the loginmodule. js file:
<script data-main="../script/app/loginModule" src="../script/thirdParty/require.js"></script>
Loginmodule. js contains a configuration and a module definition:
/*global window, document */require.config({paths: {"jquery": "../thirdParty/jquery-1.8.0.min","ajaxUtility": "./ajaxUtility","jquery.artDialog": "../../plugin/artDialog4.1.6/jquery.artDialog","jqueryTool": "./jqueryTool","login": "./login"},shim: {"jquery.artDialog": {deps: ["jquery"],exports: "artDialog"}}});define("loginModule", ["jquery", "ajaxUtility", "dialog", "jqueryTool", "login"], function ($, ajaxUtility, dialog, jqueryTool, login) {'use strict';var locale = window.locale;ajaxUtility.init(locale);$(document).ready(function () {login.init(ajaxUtility, dialog, jqueryTool, locale);});});
Note that my loginmodule is a module dependent on the dialog module.
The dialog module is another module defined by myself. It depends on the jquery. artdialog module.
The jquery. artdialog module demonstrates the dependency on jquery in shim.
Now let's take a look at the module dialog. js that depends on jquery. artdialog. The following array shows the dependency, and the parameter uses the variable name exported from exports in shim.
Define ("dialog", ["jquery", "jquery. artdialog "], function ($, artdialog) {'use strict '; return {canceltext:" ", locale:" ", oktext:" ", titletext :"", // language shoshould be either 'cn' or 'en' init: function (locale) {This. locale = locale; If (locale = "cn") {This. canceltext = "cancel"; this. oktext = "OK"; this. errortitletext = "error"; this. oktitletext = "info";} else {This. canceltext = "cancel"; this. oktext = "OK"; this. errortitletext = "error"; this. oktitletext = "info" ;}}, error: function (Message) {$. dialog ({Title: This. errortitletext, icon: "error", content: Message, okval: This. canceltext, OK: function () {This. close () ;}) ;}, done: function (Message) {$. dialog ({Title: This. oktitletext, icon: "OK", content: Message, okval: This. oktext, OK: function () {This. close ();}});}};});
It takes patience to make good use of requirejs. You only need to repeat the experiment based on your actual situation by reading the document.
By the way, you only need to add require. config... To the JS file directly referenced on the top level, that is, the HTML page. Other JS files are not needed.
If there are multiple pages, it is clear that you need multiple such top level files. Someone is called Bootstrap and the name is also suitable.