ThemeForest

Loading and calling jQuery plugins Question

204 posts Bold WordPress Theme Development
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Interviewed on the Envato Notes blog
  • Exclusive Author
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 200 and 499 users
srhnbr says

Ok so I’am using the Nivo Slider in my new WordPress theme.

I like loading scripts only where needed so I have it loading only for the frontpage and in my 1 file myjquery.js I make the call
$('#my_element').nivoSlider();

All’s good.

Now when I am on a different page (other than the frontpage) and since the nivoslider plugin does not load on other pages I get a javascript error. “nivoslider is not a function”. Of course, because the plugin is not loaded but the call is there.

Here’s my question. How do I prevent that error?

I tried (and perhaps I am completly wrong in doing this)


if ($("#my_element").length != 0) {        
    $('#my_element').nivoSlider({ pauseTime:5000});
}

thinking that checking if my_element exists and then calling the script would help but I still get the error.

What do you think it’s better to do? Take the call itself in a separate file and load that when the plugin loads? That would solve the javascript error but isn’t it a little too much to load an entire new document for just 1 or 2 lines of code?

If anyone would kindly offer a suggestion or solution I would greatly appreciate it :)

2 years ago
204 posts Bold WordPress Theme Development
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Interviewed on the Envato Notes blog
  • Exclusive Author
  • Sold between 250 000 and 1 000 000 dollars
  • Elite Author
  • Bought between 1 and 9 items
  • Europe
  • Referred between 200 and 499 users
srhnbr says

You know I am wrong! I don’t get the error. All’s good :)

2 years ago
by
by
by
by
by