Tsab ntawv teev npe
WordPressLazy loading ntawm JavaScript kom ceev cov nplooj ntawv rendering.
JavaScript's Defer attribute yog dab tsi?
Txhua tus neeg yuav tau ntsib qhov xwm txheej no:
Muaj N scripts nyob rau hauv lub taub hau, uas yuav thaiv nplooj ntawv rendering thaum thauj cov ntawv, uas feem ntau yog dawb paug.
Tau kawg, peb tuaj yeem daws qhov teeb meem no los ntawm kev muab cov ntawv sau rau hauv cov cai hauv qab.
Txawm li cas los xij, qee qhov kev loj hlob ib puag ncig tuaj yeem ua rau txoj haujlwm yooj yim no nyuaj heev.
Ntawm no peb tuaj yeem siv Defer tus cwj pwm, uas yog tus cwj pwm tsis tshua muaj nyob hauv JavaScript.
Tej zaum koj yuav tsis siv nws, tab sis kuv paub tseeb tias koj yuav tsis tso nws tom qab nyeem cov lus qhia no.
Nws lub luag haujlwm tseem ceeb yog cia cov ntawv sau tag nrho nplooj ntawv tom qab nws tau thauj khoom, es tsis yog parsing nws thaum lub sijhawm thauj khoom, uas muab tag nrho nplooj ntawv nrawm rau cov ntawv sau uas tsuas muaj qhov tshwm sim-ua rau JavaScript.
Yog lawm, yog tias Script tag muaj defer attribute, txawm tias nws tau muab tso rau hauv lub taub hau, nws yuav raug tua tom qab parsing HTML nplooj ntawv, uas zoo ib yam li tso Script nyob rau hauv qab ntawm nplooj ntawv.
Tau kawg, kev siv ncua sij hawm kuj tseem txwv. Feem ntau, ob lub ntsiab lus yuav tsum tau sau tseg:
1) Tsis txhob hu rau document.write hais kom ua nyob rau hauv ib tug defer hom ntawv thaiv;
- Vim document.write yuav tsim tawm cov teebmeem ncaj qha.
2) Tsis txhob suav nrog cov ntawv ua tiav tam sim ntawd uas yuav siv lub ntiaj teb hloov pauv lossis ua haujlwm hauv Defer scripts.
Ntxiv Defer tus cwj pwm rau tsab ntawv siv hauv WordPress
Hauv WordPress, peb tuaj yeem ua li cas ntxiv Defer cov cwj pwm rau cov ntawv siv los ntawm WordPress?
Peb tuaj yeem ntxiv cov cai hauv qab no rau lub ntsiab lus tam sim no lub functions.php cov ntaub ntawv ▼
add_filter( 'clean_url', 'wpcwl_defer_script',11,1); function wpcwl_defer_script( $url ){ if(strpos($url, '.js') === false){ return $url; } return "$url' defer='defer"; };
- Cov cai saum toj no tsuas yog ua haujlwm rau, raws liJavaScript code qhia los ntawm WordPressyuav siv tau.
Ceev faj
Kev tswj hwm nyob saib ua ntej yuav tshwm sim tsis muaj:
Yog tias koj siv cov cai saum toj no, nws yuav tsis pub dawb thaum koj qhib lub sijhawm saib ua ntej kev tswj hwm (Tshuaj → Customize), yog li thov siv nws kom tsim nyog.
Thaum yuav tsum tau saib xyuas kev tswj xyuas lub sijhawm tiag tiag, tawm tswv yim tawm cov cai saum toj no, thiab tshem tawm cov lus pom tom qab kev kho tiav tiav.
PHP comment code piv txwv:
/* 这里是代码 */
Cia siab Chen Weiliang Blog ( https://www.chenweiliang.com/ ) qhia "Yuav ua li cas kom WordPress ncua sij hawm thauj khoom JavaScript kom nrawm nplooj ntawv rendering?" 》, pab tau rau koj.
Zoo siab txais tos los qhia qhov txuas ntawm kab lus no:https://www.chenweiliang.com/cwl-954.html
Zoo siab txais tos rau Telegram channel ntawm Chen Weiliang blog kom tau txais qhov hloov tshiab tshiab!
📚 Daim ntawv qhia no muaj nuj nqis loj, 🌟Qhov no yog lub sijhawm tsis tshua muaj, tsis txhob nco nws! ⏰⌛💨
Share thiab like yog tias koj nyiam!
Koj qhov kev sib koom thiab kev nyiam yog peb qhov kev txhawb nqa tas mus li!