Build Your Own Business Website header image

Welcome to our Members Only Forum

You are welcome to view the content on our Forum, just scroll down to view this forum post.
If you like what you find and want to be able to post a question, please check out our subscription options.
Please feel free to search the forum using either the Google Site Search above or the forum search below.

Avatar
Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Best Practice: Dequeue javascript
Avatar
Aernout Zevenbergen
Member
Members

Community Member
Forum Posts: 31
Member Since:
June 30, 2012
sp_UserOfflineSmall Offline
1
October 12, 2013 - 8:40 am
sp_Permalink sp_Print

Hi, 

 

I have been building a website for an art gallery here in Nairobi (Kenya), which by now has a few hundred pages (because of the various art works). I have included several items into the site that have bloated the overall site, with javascripts working on each and every page, even where they don't have to.

 

Meteor Slides, for example, is only used on the front page.

Contact From 7 only on the contact page.

Calendar stuff only on the calendar pages.

Etcetera, etcetera, etcetera.

 

How can I make sure that scripts only run on the pages they're supposed to run on, and not on other pages?

 

All in all there are 39 (!!) javascripts running, all doing their thing with DNS searching and/or HTTP requests, slowing down every page, including those where they're not needed...  

 

I have managed to dequeue the various css-files. 

 

But with the javascript I'm not being successful. 

 

I now have this in my custom.php, grabbed from here, but adjusted to reflect the correct page ID: 

 

function wpa_scripts() { // if this is not the contact page, remove the script

if( ! is_page( '29' ) ) { 

wp_dequeue_script( 'contact-form-7' );

}

}

 

// adjust priority to make sure this runs after the plugins add their scripts/styles

add_action( 'wp_enqueue_scripts', 'wpa_scripts', 100 ); 

 

But it has no effect...

 

Does anybody have a suggestion? 

Avatar
Aernout Zevenbergen
Member
Members

Community Member
Forum Posts: 31
Member Since:
June 30, 2012
sp_UserOfflineSmall Offline
2
October 12, 2013 - 8:42 am
sp_Permalink sp_Print

I now also see that the above code wouldn't lessen the call on the HTTP requests as

"// adjust priority to make sure this runs after the plugins add their scripts/styles"...

 

Also: off topic: How can I get email notifications if an answer is posted? I can't seem to make that work on the forum. Probably not looking in the right place...

Avatar
Rick Anderson
Desert Hot Springs, CA
Admin
Forum Posts: 13637
Member Since:
November 8, 2009
sp_UserOfflineSmall Offline
3
October 12, 2013 - 9:52 am
sp_Permalink sp_Print

Actually the code will reduce http requests because no requests are generated until the page gets sent.  However there is a decent article on how to exclude the contact form 7 scripts here.

This is a serious problem and I also am looking for ways to resolve this in Thesis 2.  I will probably be developing some boxes that can be used to fix this.

Avatar
Aernout Zevenbergen
Member
Members

Community Member
Forum Posts: 31
Member Since:
June 30, 2012
sp_UserOfflineSmall Offline
4
October 13, 2013 - 3:27 am
sp_Permalink sp_Print

Thanks Rick. 

 

I'm no expert in php, and am not sure how to do this, from the link you gave:

"Note that 

wpcf7_enqueue_scripts()

 and 

wpcf7_enqueue_styles()

 must be called before

wp_head()

 is called."

 

I'll wait with most of the "script cleaning" work for the boxes you mentioned.   

Forum Timezone: America/Los_Angeles

Most Users Ever Online: 228

Currently Online:
20 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Keith W Jones: 1428

Brian Tozer: 1004

Carolin: 982

Heidi Hafner: 508

John Cunningham: 423

Reece Morrel: 353

Newest Members:

emma62755

sharonmarks

emma80233

Tijs de Jong

Daniel Frey

mutek

Forum Stats:

Groups: 8

Forums: 46

Topics: 6995

Posts: 34796

 

Member Stats:

Guest Posters: 6

Members: 6761

Moderators: 2

Admins: 2

Administrators: Rick Anderson, Rick Anderson

Moderators: Pierre Cote, Laura Nugent