175 posts
  • Has been a member for 4-5 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Europe
  • Referred between 100 and 199 users
  • Bought between 10 and 49 items
ThemePrince says

Tough question! I’m working on a full AJAX WP theme and I need some tips from the pros. How do you handle dinamically loaded Javascript functions in AJAX themes?

Let’s see two examples to understand my problem better.

Example #1: User installs a new WP plugin, e.g.: Google maps shortcode, which outputs the div of the map and some scripts after that to start the map. When I call this page (where the google map shortcode is placed) the scripts won’t (always) work.

Example #2: User installs a ToolTip plugin. But this plugin won’t print any scripts after the shortcode, instead it prints its own script in the head and executes it when the document is ready, e.g.: $(‘a.icon’).toolTip(); This script also won’t work because first I need the content elements from the AJAX call, I can apply the function after that only. Of course if I put it into success callback, it works. But should the user put every new JS function into this callback function? How should I handle this well?

Extra info which may help: I use jQuery.load() function and I only get the #content div from the loaded pages, not the whole page.

Idea as possible solution #1 (not tested yet): get script tags from AJAX loaded content and eval() them?

175 posts
  • Has been a member for 4-5 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Europe
  • Referred between 100 and 199 users
  • Bought between 10 and 49 items
ThemePrince says

Bump!

1 post
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Sold between 100 and 1 000 dollars
gravityworks says

up

1594 posts Building the Web.
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Author had a File in an Envato Bundle
  • Most Wanted Bounty Winner
  • Bought between 50 and 99 items
  • Has been a member for 4-5 years
+2 more
ZoomIt says

AJAX WP themes are very tricky.

Best thing to do is offer an option to disable the ajax behavior to a normal page load. So users that only want the theme functionality should use the ajax loading and users who have multiple plugins should use normal page load.

I always offer an option to always load scripts in my plugins ( so scripts will not load only when the shortcode is parsed, but always, in the head section ) . But this is not enough sometimes ( for your example of document ready ) . So I see no other solution for this.

175 posts
  • Has been a member for 4-5 years
  • Sold between 50 000 and 100 000 dollars
  • Exclusive Author
  • Europe
  • Referred between 100 and 199 users
  • Bought between 10 and 49 items
ThemePrince says

ZoomIt: this option is already there.. ;)

165 posts
  • Europe
  • Exclusive Author
  • Has been a member for 1-2 years
  • Sold between 10 000 and 50 000 dollars
subsolar says

While searching for possible solutions. Had someone tested this ?

115 posts A hunch is creativity trying to tell you something.
  • Elite Author
  • Won a Competition
  • Sold between 50 000 and 100 000 dollars
  • Most Wanted Bounty Winner
  • Exclusive Author
  • Has been a member for 5-6 years
  • Bulgaria
  • Referred between 50 and 99 users
+1 more
bilbo_b says

While searching for possible solutions. Had someone tested this ?
I think this solution sometimes adds duplicated JS instances/listeners which eventually will increase the cpu usage and lag of the site more and more on every click/eval(); or i’m wrong but nevertheless i’m also curious for the best solution
by
by
by
by
by
by