views:

32

answers:

1

If I want to provide a widget on my website that other people can insert on their webpages, via :

Can I import and use Jquery in that file so that the user doesn't need to add it manually to his page.

How would I do this? (I'm new to Javascript too)

Thanks

A: 

You could do a simple copy and paste (i.e. copy the code in the jQuery file into your own JavaScript file).

If you wanted to keep the files separate, however, you could add the jQuery script to the DOM as follows:

var script = document.createElement("script");
script.src = "path/to/jQuery.js";
document.getElementsByTagName("head")[0].appendChild(script);

Steve

Steve Harrison
thanks steve for your fast response. can I also ask:- is this bad practice?- if they have already got jquery loaded, will this cause a conflict? and should i check first and how?- do I have to do anything to prevent it conflicting with other libraries they may have loaded?thanks
I'm not sure about the first; for the second, see http://docs.jquery.com/Using_jQuery_with_Other_Libraries.
Steve Harrison