2016-02-05 117 views
1

使用該函數排隊外部腳本是正確的嗎? 它的工作原理,但不會減緩打開網站?我應該使用wp_enqueue_script()來封裝外部腳本嗎

wp_register_script('google-maps', 'http://maps.googleapis.com/maps/api/js?sensor=true', null, null, true); 
wp_register_script('jsapi', 'https://www.google.com/jsapi', null, null, true); 
wp_register_script('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', null, null, true); 
wp_register_script('unveil', get_template_directory_uri() . '/new-js/jquery.unveil.min.js', null, null, true); 

回答

3

不,它不會減慢網站(至少不足以使其成爲不使用它的原因)。這是在WordPress中排列任何類型腳本的正確方法。所有這些功能都會將適當的<link>標籤(與依賴關係)添加到您的HTML <head>

但是,出於某種原因,您沒有在代碼中包含依賴項。例如,引導jQuery的需要,所以你應該包括在你的函數:

wp_enqueue_script('bootstrap', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array('jquery'), '3.3.5', true); 

如上圖所示,你也應該考慮使用wp_enqueue_script()代替wp_register_script(),因爲這將節省您的一步。

+0

感謝您的快速回答。我問過外部腳本減慢整個頁面的速度(阻止其他腳本)時遇到問題。我有facebook,地圖和bootstrap的問題。有簡單的解決方案嗎? –

相關問題