How do I load an HTML page in a
using JavaScript?

后端 未结 15 2260
醉话见心
醉话见心 2020-11-22 08:29

I want home.html to load in

15条回答
  •  清歌不尽
    2020-11-22 09:19

    I saw this and thought it looked quite nice so I ran some tests on it.

    It may seem like a clean approach, but in terms of performance it is lagging by 50% compared by the time it took to load a page with jQuery load function or using the vanilla javascript approach of XMLHttpRequest which were roughly similar to each other.

    I imagine this is because under the hood it gets the page in the exact same fashion but it also has to deal with constructing a whole new HTMLElement object as well.

    In summary I suggest using jQuery. The syntax is about as easy to use as it can be and it has a nicely structured call back for you to use. It is also relatively fast. The vanilla approach may be faster by an unnoticeable few milliseconds, but the syntax is confusing. I would only use this in an environment where I didn't have access to jQuery.

    Here is the code I used to test - it is fairly rudimentary but the times came back very consistent across multiple tries so I would say precise to around +- 5ms in each case. Tests were run in Chrome from my own home server:

    
    
    
        
    
    
        

提交回复
热议问题