0
我試圖在組件中動態加載js腳本。檢查組件Angular 2中是否已有外部js腳本
我有一個使用驗證碼,這個問題與它的是每次用戶訪問該組件,外部腳本加載聯絡組件。
export class ContactCmp {
captchaReady = false;
constructor() {
}
ngOnInit() {
if(){ //check if script is already loaded
this.loadScript('https://www.google.com/recaptcha/api.js', this.captchaLoaded);
}
else{
console.log("captcha is already loaded.")
}
}
captchaLoaded(){
console.log("Captcha loaded");
this.captchaReady = true;
}
loadScript(url, callback)
{
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onreadystatechange = callback;
script.onload = callback;
head.appendChild(script);
}
}
如何檢查腳本是否已經加載?
的存在,如果它的緩存,這一點我敢肯定,谷歌將確保它是,那麼它應該立即加載... – dandavis
@ dandavis它沒有加載正確的方式 –