I'm scraping a news website with PHP and injecting the Jquery library in the head, along with my own script which depends on Jquery. I have experienced some loss of functionality regarding some websites. So I was wondering if it would be possible to make the Jquery library function only for my script and thus not "assimilating" itself with other scripts on the page.
I have Googled for this but all I can find is Jquery noConflict() but it doesn't get the job done.
jQuery noConflict is the way to go but remember you will need to name your no conflict copy and then use this copy of jQuery for your code:
var $j = jQuery.noConflict();
// $j is now an alias to the jQuery function; creating the new alias is optional.
$j(document).ready(function() {
$j( "div" ).hide();
});
See more here: http://learn.jquery.com/using-jquery-core/avoid-conflicts-other-libraries/
What about editing jQuery library itself so it is called something else.
Embed the jQuery object via PHP to not have this huge chunk of jQuery code (more readable if you can inject with PHP, but not required, or use a script tag that points to your server).
Customize jQuery to be not "jQuery" or "$".
Then, to customize jQuery, download jQuery and look at the very bottom, it says:
// Add your customized jquery here
window.jQuery = window.$ = jQuery;
Change this line to something else:
// Expose jQuery to the global object
window.myQuery = window.$$$$$$$$$ = jQuery;
Related
We have a page which conatins jQuery and third party JavaScript API. This api inserts jQuery dynamically during run-time into the page. It causes conflictions.
How can I prevent the third party API to add jQuery to the page?
My answer should be applied by the them not you, first Rule for Third-Party Javascript is:
"You Don't own the website"
so if they want to use jQuery then they have to use jQuery noConflict that is the best way to deal with a website working with multiple jQuery versions (Yours and theirs):
Completely move jQuery to a new namespace in another object.
var j = jQuery.noConflict(true);
// then you can say
j( "div p" ).hide();
if you add this line and then define a new jQuery then j will be an alias for old version and jQuery - $ will be an alias for the new version by default.
if you want to make sure; you can check the versions of jQuery present in your page:
j.fn.jquery // this should show you your jQuery version
jQuery.fn.jquery // this should show you their jQuery version
you can use it if you still need to use this third-party javascript and no way for them to change their code you can read more about jQuery noconflict
You can assign jQuery to a namespace you use as: mycode.$ and mycode.jQuery so you can later use:
mycode.$("div p")
this is always safer to use namespace to make sure no one else write in same code may override your variables.
There isn't really a way to stop a script from running that you pull in. I was dealing with something similar yesterday. There are ways to overwrite the changes it does obviously but there is no dynamic way to just stop it from running other than to just not pull it in.
I am developing a new JS library. I want to include JQuery as core part of it.
My library is supposed to work on all kind of websites, so in those sites JQuery may be already included with different version , may have some other library which may conflict with JQuery inside my library.
jQuery.noConflict();
may not be a complete solution , since it will remove variables from the global scope. My library don't want to change any other settings by user.
So how can i avoid these issues
Conflict with other library ? ($ alias - may be jQuery.noConflict();).
Conflict with other versions of JQuery , if it is already included in the web page.
Thanks in advance. Please don't tell that not include JQuery , use normal JS :)
It's very simple.
First load your preferred jQuery, and get a handle to that by omitting any conflicts from already existing jQuery by using,
$yourJQ = window.jQuery.noConflict(true);
Use $yourJQ in a anonymous scope in all the functions that you want use your jQuery. Like this,
(function($){
// external library code
})($yourJQ);
Hope this helps
Also visit http://alexmarandon.com/articles/web_widget_jquery/
Well regardless of whether you remove variables from the global scope or not you will have to do a find & replace to change which library points at which version of jQuery; because they will either use $ or jQuery & conflict in the global scope, anyway. You could try something like this:
Load first version of jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.js"></script>
<script type="text/javascript">
jQuery.noConflict();
(function(window,$) {
window.jQuery_v1 = $;
})(window,jQuery);
delete jQuery;
</script>
Load second version of jQuery
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script>
<script type="text/javascript">
jQuery.noConflict();
(function(window,$) {
window.jQuery_v2 = $;
})(window,jQuery);
delete jQuery;
</script>
Use both versions of jQuery
<script type="text/javascript">
// this is now one version of jQuery
console.log(jQuery_v1);
// this is now another version of jQuery
console.log(jQuery_v2);
</script>
I'm considering loading jquery and a series of plugins via a loader i.e labjs, or yepnope.js.
I want to load the jquery plugins into a custom jquery namespace, if possible without modifying them.
Any idea how I could load these plugins without having to modify them by adding a (mynamespace.Jquery) efficiently?
How you can load them into the namespace is easy enough:
(function(jQuery) {
-- Plugin code here
})(mynamespace.jQuery);
The second part of your question is a little harder. Say your plugin resides on the server at http://www.mysite.com/javascript/jquery.fancybox.js
You're going to have to serve the plugin. You're going to have to dynamically wrap the plugin somehow within your desired namespace.
Quick pseudo code answer in php:
An example request would be to
http://www.mysite.com/plugin.php?script=jquery.fancybox&namespace=mynamespace
In plugin.php, read in the JS file contents from the
$_REQUEST['script'] param
Output the file contents in the wrapper, e.g.
(function(jQuery) {
< ?= $scriptFileContents ?>
})(< ?= $_REQUEST['namespace'] ?>.jQuery);
And obviously you're going to escape the input parameters to not allow for XSS attacks, right?
I am in big trouble at the moment. We have a huge JS library that I need to maintain. It uses internally jQuery 1.6.2.
The client where we installed the library uses jQuery 1.3.4 and the fancybox overlay plugin.
After loading these two, he simply throws in a
jQuery.noConflict();
but without saving his jQuery to a variable (namespacing).
Now I need to access his fancybox, but if I use
$.fancybox({...})
or
jQuery.fancybox({...})
I get in both cases an "is not a method error".
I can duplicate the error on my local machine and it would not appear without the jQuery.noConflict(); statement.
We are also doing a noConflict with our jQuery but we save it to another varieable, i.e.
jq162 = jQuery.noConflict();
The problem is the customer is of course unwilling to change anything of his code.
Is there any way how I can access his jQuery / Fancy after this statement and after loading our 1.6.2?
thanks in advance...
UPDATE
the scripts are loaded in the following order:
// client
jquery 1.4.2
jquery fancybox
<script type="text/javascript"> jQuery.noConflict(); </script>
jQuery 1.2.6 which seems to be necessary for Liferay
// now comes my library
jQuery 1.6.2
my scripts
i know, if we could change step 3 to
<script type="text/javascript"> $jq = jQuery.noConflict(); </script> it would work, but right now that is out of my influence.
in 6. myscripts I need to access the fancybox from 2.
any ideas?
It shouldn't be a problem. You must be loading your scripts after the client's scripts (if you're loading yours first, there shouldn't be any problem, your jquery is namespaced, and the clients version will be in jQuery along with the plugin).
So simply namespace his jQuery object before you load your script:
<script>
jq132 = jQuery;
</script>
<script src="yourScripts"></script>
<script>
jq162 = jQuery.noConflict();
console.log(jq132.fancybox);
</script>
UPDATE
As per your update, what you're trying to do is impossible. There is no longer a reachable reference to that jQuery/plugin instance (unless fancybox accidentally leaked a global reference, which I highly doubt). I don't know fancybox, although it's possible that the functionality isn't instance-specific. So it may be possible to just reattach fancybox to your version of jquery, and it will be able to perform all the necessary things. What I said about the reference however, remains true.
Obviously adding a few characters like you suggested (or other similar ways) would solve the problem. But if that is impossible, then your client will have to realise that. It should be proof enough if you simply ask them to access there own plugin under the same conditions - i.e. without changing code.
They should probably have a long and hard think about their entire project. Having to load three different versions of the same product is a sign that something is very very wrong.
I've got the following situation
A rails application that makes use of rjs / Scriptaculous to offer AJAX functionality
Lot of nice javascript written using jQuery (for a separate application)
I want to combine the two and use my jQuery based functionality in my Rails application, but I'm worried about jQuery and Scriptaculous clashing (they both define the $() function, etc).
What is my easiest option to bring the two together? Thanks!
jQuery.noConflict();
Then use jQuery instead of $ to refer to jQuery. e.g.,
jQuery('div.foo').doSomething()
If you need to adapt jQuery code that uses $, you can surround it with this:
(function($) {
...your code here...
})(jQuery);
I believe it's jQuery.noConflict().
You can call it standalone like this:
jQuery.noConflict();
jQuery('div').hide();
Or you can assign it to another variable of your choosing:
var $j = jQuery.noConflict();
$j('div').hide();
Or you can keep using jQuery's $ function inside a block like this:
jQuery.noConflict();
// Put all your code in your document ready area
jQuery(document).ready(function($){
// Do jQuery stuff using $
$("div").hide();
});
// Use Prototype with $(...), etc.
$('someid').hide();
For more information, see Using jQuery with Other Libraries in the jQuery documentation.
jRails is a drop-in replacement for scriptaculous/prototype in Rails using the jQuery library, it does exactly what you're looking for.