简体   繁体   中英

Load font for website for the whole session using Javascript

I am making a website in hindi, for which i have to load font from google fonts using javascript.
I want to load the font for whole session only once on the start page, what javascript code should i use??
I use this code to load font on each page:

WebFontConfig = {
google: { families: [ 'Jaldi::devanagari' ] },
active: fontLoaded
};
(function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') + '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
})();

Browsers cache resources (unless caching has been disabled manually). That means, as long as the browser has the font in its font cache, adding the resource will not make the browser download the font again. It will instead use the font from the cache. (The same is true for JavaScript files, so the webfont.js script will also be cached).

Note however, that you still need to make the browser execute the script, so you need to add the script tag on every page where you need the font (as you are doing).

tl;dr: You don't need to change anything (as far as I can tell from your question at least).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM