MyBB Community Forums

Full Version: best way to test is jquery already loaded or listed in header
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am thinking of adding jquery to one of my plugins but need the best way to know if I need to add the jquery lines to the header or not. Since I know its added by some users and some themes, it may or may not already be inserted.

I assume inserting a placeholder into header <--myjquery--> and then doing this in global_end is the simplest way, but is it efficient or the best solution?

$myjquery_bit = '<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.3/jquery.min.js"></script>';
if(!stristr($header, 'jquery'))
{
    $header = str_replace('<--myjquery-->', $myjquery_bit, $header);
}
when installing your plugin check if jQuery is already loaded (like you do on here) and if it isn't, then insert your code into the template. Doing it that way just uses hooks for no reason Toungue
since my plugin is standalone pages, I can include it in my templates, but if someone adds it later to headerinclude, then can always remove it from my template.

I think that would be the simplest solution.
Maybe in your plugin settings have an option to enable/disable it? Wouldn't really work if you included it in the template though :\
i am going to put it directly in the template specific to my plugin that includes the <head> tag. If a user adds jquery later or has it already,they can simple edit the template and remove my line.
Make a $() call (to a jQuery function, of course) in a try catch block. If it fails, load jQuery.
meh, too much work for me. i have updated my templates and leave it to the user to remove the additional call to load the jQuery API.