7

Possible Duplicate:
Dynamically Including jQuery using JavaScript if it's not already present

I am making a site similar to google analytics in that I have my users paste a bit of javascript code on their site. My Question is how can I manage to include JQuery in my javascript snippet of code?

I already know how to include files using js: How do I include a JavaScript file in another JavaScript file?

But how do I make sure that there is no JQuery conflicts?

Gold star to anyone who suggests good practices in terms of generating embedded javascript code:)

0

4 Answers 4

4

The short answer is: You don't.

You should not impose a framework upon your users. Google does not do it with analytics, and you should not either. Use basic java script functionality only and write from scratch or copy what you need from other projects (as long as licenses permit).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that makes sense. I will begin rewriting my code now...
0

Always test that whatever you include isn't already defined:

if(typeof jQuery === "undefined") {
   // include jQuery
}

3 Comments

That is not a useful check. The external site may have jQuery 1.7, while you're depending on functionality from jQuery 1.8.
well sure it could be made more robust. We don't really know the requirements.
We know the code is required to run on sites that we don't control.
0

I will suggest to use the jQuery no conflict function.

http://uzbekjon.hubpages.com/hub/How-to-use-jQuery_noConflict

Hope, it will help

Comments

0

There is no way to do this without conflicts with userscripts.

Read this discussion to learn how to write code without jQuery: is there an easy way to convert jquery code to javascript?

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.