Embodiment
For the purpose, technical scheme and the advantage that make the embodiment of the present invention clearer, below in conjunction with the accompanying drawing in the embodiment of the present invention, technical scheme in the embodiment of the present invention is clearly and completely described, obviously, described embodiment is the present invention's part embodiment, rather than whole embodiment.Embodiment based in the present invention, those of ordinary skills, not making under the creative work prerequisite the every other embodiment obtained, belong to the scope of protection of the invention.
The user carrys out browsing page by browser usually, and browser receives page load request, loads webpage corresponding to this page load request, carries out the source file of webpage.Source file is generally the code that HTML writes, and in source file, can embed the script reference code, to realize the loading to script.It is a kind of pattern in the Web exploitation that inertia loads (Lazy Loading), and under default situations, partial page data or script do not download to browser, when user's triggering or application logic triggering, then downloads the data of response to server request.Inertia by script loads, and can when needs be carried out script, again this script be downloaded and carry out, and avoids taking of Internet resources bandwidth.
Embodiment mono-
The script loading method process flow diagram that Fig. 1 provides for the embodiment of the present invention one.As shown in Figure 1, the script loading method that the present embodiment provides specifically can be applied to script loading processing process, especially can be applied to the processing procedure of browser to the script of inertia loading, and script is specifically as follows the JavaScript script.The script loading method that the present embodiment provides can be carried out by the script charger, in this charger, can be provided with browser, can realize by the mode of software and hardware.When the user passes through the browser browsing page, browser receives page load request, Web page loading, the source file of execution webpage.If it is that inertia loads that the programmer arranges partial page data and the script of this webpage in the webpage development process, in initial webpage loading procedure, browser only will need web data and the script downloaded at once to download to this locality, be stored in the script storage unit, and the page data and the script that these are not needed inertia to load download to this locality.The script storage unit specifically can be arranged in this charger and also can arrange separately.
In the process of the source file of browser execution webpage, service logic judgement in webpage triggers or clocked flip, during perhaps by the manual clicking trigger of user, need to call the call method of realizing this function in realizing the process of certain function, this call method is specifically as follows a function, browser generates the logic of business functions trigger request of calling this call method in order to indication, carries the method name of this call method in this logic of business functions trigger request.
The script loading method that the present embodiment provides specifically comprises:
Step 10, when the script charger receives the logic of business functions trigger request of the method name that carries call method, judge in the script storage unit and whether have the script that described call method is corresponding.
Concrete, call method is recorded in script usually, at first according to method name, the script of storing in the script storage unit is inquired about, judge in the script storage unit and whether have the script that this call method is corresponding, if there is this script in this script storage unit, illustrate that script that this call method is corresponding has downloaded to the script storage unit in initial page loading procedure.This script is resolved, carried out the call method in script, to realize corresponding function.If do not have this script in this script storage unit, illustrate that the script that this call method is corresponding is the script that inertia loads, this script is not also downloaded and is stored in the script storage unit.
If do not have described script in the described script storage unit of step 20, this script charger obtains according to described method name the script reference code that described call method is corresponding;
If there is not this script in the script storage unit, the corresponding script reference code according to this call method of the method name acquiring.The script reference code is specially one section code, by carrying out this code, and can be to download and the execution that realizes this script.This script reference code can obtain the source file corresponding from webpage, also can generate according to the method title, the mapping relations table of all right method to set up title and script reference code, with according to the method name acquiring script reference code, the concrete obtain manner of script reference code is not limited with the present embodiment.
Step 30, carry out described script reference code, download and resolve script corresponding to described script reference code, carry out the described call method in described script.
Browser is carried out this script reference code, downloads and resolve the script that the script reference code is corresponding.Specifically can obtain this script by the mode that sends HTML (Hypertext Markup Language) (Hyper Text Transfer Protocol is called for short HTTP) request to server.Carry out the call method in this script, and show execution result to the user, to realize corresponding logic of business functions.
Below take the implementation procedure of the script loading method that the JavaScript script provides the present embodiment as example and elaborate:
The user is by the browser browsing page, and browser is carried out the source file of webpage, downloading web pages data and script, and script is stored in the script storage unit.When user's clicking trigger service logic need to be called call method com.huawei.iread.helloWord (), generate the logic of business functions trigger request.Carry the method name of call method in this logic of business functions trigger request, for com.huawei.iread.helloWord, according to naming rule, the script that records the JavaScript script of this call method is com.huawei.iread.js, the title that the character field com.huawei.iread except last character section of method name is the JavaScript script that call method is corresponding.According to the method title, the script of storing in the script storage unit is inquired about, if there is not this JavaScript script in the script storage unit, the corresponding script reference code according to this call method of the method name acquiring.
Particularly, the script reference code can pass through the JavaScript script of the mode quote server end of Script mark.Concrete form is as follows:
<script?type=″text/javascript″src=″../script/com.huawei.iread.js″>
Wherein, in the Script mark, the type attribute is in order to indicate the mime type of script.The URL(uniform resource locator) (Uniform/Universal Resource Locator be called for short URL) of src attribute in order to indicate external scripts.Com.huawei.iread.js is the JavaScript script that call method is corresponding, the store path that URL is this JavaScript script.
Browser is carried out this script reference code, downloads and resolve JavaScript script com.huawei.iread.js, carries out call method com.huawei.iread.helloWord ().
The script loading method that the present embodiment provides, when receiving the logic of business functions trigger request of the method name that carries call method, judge in the script storage unit and whether have the script that this call method is corresponding, if do not exist, the corresponding script reference code according to the method name acquiring call method.Carry out again this script reference code, download and resolve the script that the script reference code is corresponding, carry out the call method in this script.Because browser is the mode of obtaining the script reference code that call method is corresponding, finally realize the execution of call method by carrying out this script reference code, avoided just reserving the entrance of call back function in the initial script of webpage when web application is developed, and need clearly specify and need inertia to load which script, and the approximately qualitative and higher defect of coupling of the AM/BAM caused has improved versatility and the dirigibility of Web application and development.
Embodiment bis-
The script loading method process flow diagram that Fig. 2 provides for the embodiment of the present invention two.As shown in Figure 2, the script loading method that the present embodiment provides is on the basis of the described embodiment of Fig. 1, and preferably, script reference code corresponding to script that inertia loads can be recorded in the source file of webpage.The script of in this script reference code, by the mode that sign is set, indicating this script reference code to quote is that inertia loads.
For example, take the JavaScript script as example, can realize by the mode of expansion Script mark additional information the inertia of JavaScript script is loaded.Can define the defer attribute in the Script mark, the defer attribute is in order to indicate whether that execution is postponed to script, until the page loads.Include the defer attribute in the script reference code, and the value of defer attribute is " defer ", downloads this script in the process loaded at the page, but do not carry out immediately this script, until page loaded is just carried out this script.Can preset when the value of defer attribute is other values except " defer ", the indication inertia loads this script.This script reference code is specifically as follows:
<script?type=″text/javascript″defer=″a″src=″../script/com.huawei.iread.js″>
In the present embodiment, step 10, when receiving the logic of business functions trigger request of the method name that carries call method, before judging in the script storage unit and whether having the script that described call method is corresponding, specifically can also comprise the steps:
Step 40, when receiving page load request, carry out the code in the source file of the page corresponding to described page load request, wherein, during the sign that includes if recognize in the script reference code of described source file to indicate inertia to load, skip described script reference code.
The request of the request browser Web page loading page generated when page load request is the user and browses web sites by browser.When browser receives page load request, carry out the code in the source file of the page corresponding to this page load request, carry out initial page load operation.In the implementation of code, include the sign of indicating inertia to load if recognize in the script reference code, as defer=" a ", illustrate that the script in this script reference code needs inertia to load, skip this script reference code and do not carry out.By being set in the script reference code, sign loads with the indication inertia, and just can be by this script reference code record in source file, for follow-up obtaining of script reference code provided convenience when webpage development.
In the present embodiment, carry out the code in the source file of the page corresponding to described page load request, specifically can also comprise the steps:
During the described sign that do not comprise if recognize in the script reference code of described source file to indicate inertia to load, carry out described script reference code, download script corresponding to described script reference code, and described script is stored in described script storage unit.
In initial page load operation, do not comprise the sign of indicating inertia to load if recognize in the script reference code, illustrate that the script that this script reference code is corresponding can download at once, download this script, and this script is stored in the script storage unit, while with convenient logic of business functions triggering, calling the call method in this script, can be searched to obtain corresponding script to the script storage unit.
Step 20, if do not have described script in described script storage unit, obtain according to described method name the script reference code that described call method is corresponding, specifically can comprise the steps:
If do not have described script in the described script storage unit of step 201, according to described method name, the code in source file searched;
If have the script reference code be complementary with described method name in the described source file of step 202, obtain described script reference code.
In above-mentioned embodiment, script reference code record corresponding to the script that inertia loads, in the source file of webpage, but do not carried out this script reference code in the original upload of the page.While not having script corresponding to the call method that need call in the script storage unit, can to the code in source file, be searched according to the method name of this call method, to obtain the script reference code that this script is corresponding.
Embodiment tri-
The script loading method process flow diagram that Fig. 3 provides for the embodiment of the present invention three.As shown in Figure 3, the script loading method that the present embodiment provides, step 20, if do not have described script in described script storage unit, obtain according to described method name the script reference code that described call method is corresponding, specifically can comprise the steps:
If do not have described script in the described script storage unit of step 203, obtain mapping table, wherein, store the mapping relations of the script information of described method name and described script in described mapping table, described script information comprises script name and store path;
In the source file of webpage, also can indicate by other means inertia to load, as do not recorded script reference code corresponding to script that needs inertia to load in source file.Owing to there not being this script reference code in source file,, in initial page loading procedure, this script reference code is not performed, and can realize the inertia of script is loaded, and the method realizes simple.
While not having this script reference code in source file, obtain mapping table.Store the mapping relations of the script information of method name and script in this mapping table, this script information comprises script name and store path.For example method name is com.huawei.iread.helloWord, corresponding with script name com.huawei.iread and store path ../script/.
Step 204, according to described method name, described mapping table is searched, obtained script information corresponding to described method name;
Step 205, according to described script information, build described script reference code.
Can get the script information of script according to method name, again because script information has comprised script name and store path, can build the script reference code according to script name and store path.
It should be noted that in the present embodiment, can, according to making an appointment, by the mode that builds the script reference code according to script information, obtain this script reference code.Also can at first according to method name, to source file, be inquired about, during the method title is complementary if be present in source file script reference code, by perform step 201 and step 202 realize obtaining of script reference code, if, while not having the script reference code be complementary with method name in source file, the method that can provide by the present embodiment is obtained the script reference code.
Embodiment tetra-
The script charger structural representation that Fig. 4 provides for the embodiment of the present invention four.As shown in Figure 4, the script charger that the present embodiment provides specifically can be realized each step of the script loading method that any embodiment of the present invention provides, and this repeats no more.
The script charger that the present embodiment provides specifically comprises judging unit 12, script reference Code obtaining unit 13 and code performance element 14.Judging unit 12, for when receiving the logic of business functions trigger request of the method name that carries call method, judges in script storage unit 11 and whether has the script that described call method is corresponding.If there is not described script in script reference Code obtaining unit 13 for described script storage unit 11, according to described method name, obtain the script reference code that described call method is corresponding, code performance element 14 is for carrying out described script reference code, carry out described script reference code, download and resolve script corresponding to described script reference code, carry out the described call method in described script.
The script charger that the present embodiment provides, judging unit 12, when receiving the logic of business functions trigger request of the method name that carries call method, judges in script storage unit 11 and whether has the script that described call method is corresponding.If do not have described script in described script storage unit 11,13 of script reference Code obtaining unit obtain according to described method name the script reference code that described call method is corresponding, code performance element 14 is carried out described script reference code, carry out described script reference code, download and resolve script corresponding to described script reference code, carry out the described call method in described script.Setting by judging unit 12, script reference Code obtaining unit 13 and code performance element 14, to obtain the mode of the script reference code that call method is corresponding, finally realize the execution of call method by carrying out this script reference code, avoided just reserving the entrance of call back function in the initial script of webpage when web application is developed, and need clearly specify and need inertia to load which script, and the approximately qualitative and higher defect of coupling of the AM/BAM caused has improved versatility and the dirigibility of Web application and development.
Embodiment five
The script charger structural representation that Fig. 5 provides for the embodiment of the present invention five.As shown in Figure 5, the script charger that the present embodiment provides is on the basis of the described embodiment of Fig. 4, and described script reference Code obtaining unit 13 specifically can comprise searches subelement 131 and Code obtaining subelement 132.There do not is described script if search subelement 131 for described script storage unit 11, according to described method name, the code in source file is searched.If there is the script reference code be complementary with described method name in Code obtaining subelement 132 for described source file, obtain described script reference code.By searching the setting of subelement 131 and Code obtaining subelement 132, can from the source file of the page, obtain this script reference code, improved the convenience of script reference Code obtaining.
In the present embodiment, this script charger specifically can also comprise source file performance element 15, source file performance element 15 is for when receiving page load request, carry out the code in the source file of the page corresponding to described page load request, wherein, during the sign that includes if recognize in the script reference code of described source file to indicate inertia to load, skip described script reference code.By being set in the script reference code, sign loads with the indication inertia, and just can be by this script reference code record in source file, for follow-up obtaining of script reference code provided convenience when webpage development.
In the present embodiment, when if described source file performance element 15 does not also comprise to indicate the described sign of inertia loading for the script reference code that recognizes described source file, carry out described script reference code, download script corresponding to described script reference code, and described script is stored in described script storage unit 11.In initial page load operation, do not comprise the sign of indicating inertia to load if recognize in the script reference code, download this script, and this script is stored in script storage unit 11, while with convenient logic of business functions triggering, calling the call method in this script, can be searched to obtain corresponding script to script storage unit 11.
Embodiment six
The script charger structural representation that Fig. 6 provides for the embodiment of the present invention six.As shown in Figure 6, the script charger that the present embodiment provides is on the basis of the described embodiment of Fig. 4, and described script reference Code obtaining unit 13 specifically can comprise that mapping table obtains subelement 133, script information obtains subelement 134 and code construction subelement 135.If mapping table obtains subelement 133 and does not have described script for described script storage unit, obtain mapping table, wherein, store the mapping relations of the script information of described method name and described script in described mapping table, described script information comprises script name and store path.Script information obtains subelement 134 for according to described method name, described mapping table being searched, and obtains script information corresponding to described method name.Code construction subelement 135 is for building described script reference code according to described script information.
If there is not this script reference code in source file, can obtain this script reference code by the mode that builds the script reference code.Perhaps, if the form of webpage development agreement is, in source file, do not record script reference code corresponding to script that inertia loads, also can obtain this script reference code by the mode that builds the script reference code.
The script loading method that the embodiment of the present invention provides and device, when receiving the logic of business functions trigger request of the method name that carries call method, judge in the script storage unit and whether have the script that this call method is corresponding, if do not exist, the corresponding script reference code according to the method name acquiring call method.Carry out again this script reference code, download and resolve the script that the script reference code is corresponding, carry out the call method in this script.Because browser is the mode of obtaining the script reference code that call method is corresponding, finally realize the execution of call method by carrying out this script reference code, avoided just reserving the entrance of call back function in the initial script of webpage when web application is developed, and need clearly specify and need inertia to load which script, and the approximately qualitative and higher defect of coupling of the AM/BAM caused has improved versatility and the dirigibility of Web application and development.Be directed to the relatively limited mobile terminal of hardware resource, the technical scheme that can adopt the present embodiment to provide realizes the inertia of script is loaded, and on the basis of realizing complicated Web application, avoids the waste of system resource and the consumption of the network bandwidth.
One of ordinary skill in the art will appreciate that: realize that the hardware that all or part of step of said method embodiment can be relevant by programmed instruction completes, aforesaid program can be stored in a computer read/write memory medium, this program, when carrying out, is carried out the step that comprises said method embodiment; And aforesaid storage medium comprises: various media that can be program code stored such as ROM, RAM, magnetic disc or CDs.
Finally it should be noted that: above embodiment only, in order to technical scheme of the present invention to be described, is not intended to limit; Although with reference to previous embodiment, the present invention is had been described in detail, those of ordinary skill in the art is to be understood that: its technical scheme that still can put down in writing aforementioned each embodiment is modified, or part technical characterictic wherein is equal to replacement; And these modifications or replacement do not make the essence of appropriate technical solution break away from the scope of various embodiments of the present invention technical scheme.