My employer is blocking the Google CDN domain that provides the jQuery file to so many websites:
http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js
I want to use GreaseMonkey to provide that jQuery reference from a different domain. Is this possible? Can I use a GreaseMonkey script to tell a page to load it's jQuery reference from this URL instead?:
http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js
You could have GreaseMonkey add it's own javascript reference, but the problem will likely be that your jQuery code will have already run before jQuery is loaded. Is it possible to call the jQuery code after your GreaseMonkey script loads jQuery again? It seems that you'd have to restart execution of the jQuery code on the page in order for this to get the results you're looking for.
Greasemonkey acts when the DOM has already loaded. You can try to rewrite the code before it gets rendered with a full fledged browser extension. Look into LiveHttpHeaders. I think it may be useful because it intercepts the page before getting to the browser. Based on that, you can then rewrite the source to suit your needs
Related
I want to create my own Userscript for Tampermonkey for a specific site I often use and I want to add some features and I want to improve some things.
Since it loads some .js files, that do some stuff I don't want to happen, I want to replace these .js files and reimplement the things I need within my Userscript.
I don't want to replace functions, I want to completely remove specific .js files from the DOM before they get executed and reimplement them in my Userscript to my preferences.
I tried to use this method: http://userscripts-mirror.org/scripts/show/125936
But it didn't work at all, no errormessages or similar, it just doesn't execute the Eventlistener, I use Chrome, could it be that this has to be implemented in another way for Chrome?
beforescriptexecute event referenced in your question isn't implemented in Chrome and probably never will be.
There is no way for a userscript in Chrome to prevent some webpage script that is referenced in the page html <script> tag from loading.
The only solution is to use an extension that blocks urls via webRequest API.
If you don't mind making your userscript dependent on another extension then use HTTP Request Blocker, Requestly and others.
For a complete solution without dependencies you'll have to make your own extension, not a userscript.
I have an Asp.NET MVC 4.0 site that uses jQuery 1.11.1. I had an issue where some of the javascript on an end users browser (IE) was not working. For some reason, they had version 1.7.x of jQuery. I've been coding for a long time, but I'm relatively new in the web development area.
Why would the browser not download my copy of jQuery from the server? Is there a way to force the browser to get my version?
Thanks!
If the client is loading your site normally (calling an endpoint on your server) then there is no way they could load any version of jquery other than the one you have included in the script tag on your web page.
If you are doing something fancy like providing a widget that your user embeds in a page then indeed, depending on the order in which the scripts get loaded, the browser might wind up with a different version of jquery.
If you're doing the latter there are methods you can use to get a particular version of jquery for your code. See here for a start:
Include a specific version of JQuery and a plugin without conflicting with the page's JavaScript library?
Based on the comments below, you might also have other libraries in your application that also load jquery. The $ variable will get the last version of jquery to load. I would have thought that every browser would load them in the same order, but perhaps on older copies IE things happen differently (or perhaps there are paths through your app that load things in a different order).
If any of that's true then you'll have to use one of the techniques above. I'd also look into whether you can force kendo not to take over $.
I'd like to create Chrome bookmarks that perform actions when clicked. The vast majority of them will be manipulating the URL and reloading the page. Can you make Chrome bookmarks that contain large amounts of Javascript? Maybe even jQuery?
The better approach would be using javascript to load an external script and append it to the <head> of the document, this, IMO, makes it easier to work with your script as only the call to load the resources needs to be bookmarklet'd*.
Example : Load external jQuery script via Bookmarklet
For example, if you wanted to load jQuery onto the page you're looking at, you could run this bookmarklet from your bookmark bar. (Disclaimer: Best to do a check for jQuery already on the page to avoid conflict. More on that here ).
javascript(function({var%20external_script=document.createElement('script');20external_script.type='text/javascript';20external_script.src='http://code.jquery.com/jquery-latest.js';document.getElementsByTagName('head')[0].appendChild(20external_script)})();
*Note that it has been minified and URL encoded. If you use Textmate, there's an option to "Copy Javascript to clipboard as bookmarklet" which does the minifying and encoding automatically.
Worth pointing out that you can load multiple resources into the DOM with this method, including stylesheets.
Is there a way to download javascript without executing it? I want to decrease my page load times so am trying to "lazy load" as much javascript onto the page while the user is idle. However I don't want the javascript to execute, I just want it to be in the browser cache.
Should I use an object tag? I noticed that I can use a LINK tag but that makes the browser think it's css which has a negative impact on my ui perf / responsiveness.
As long as you have all code in functions or classes and nothing in global scope nothing will execute.
You can then start your script with a call from
window.load(function() { //your initialisation here });
This will let the whole page load before running any scripts.
You could also add script references via script to make sure they load after any images in the page.
Just add a script element to head using script and it will load.
These pages has examples for this:
http://unixpapa.com/js/dyna.html
http://www.javascriptkit.com/javatutors/loadjavascriptcss.shtml
This way if you have a slow connection or a sever that is overloaded, the visible elements will be loaded first by the browser.
As far as I know, there is no cross-browser compliant way to get around JavaScript loading in serial. If your javascript does something when it is loaded, you need to refactor your code. For instance, you don't write your jQuery commands/actions/code in the jQuery library script; you link the jQuery library and then put your jQuery commands into a separate file. You should do the same thing with your custom libraries. If this isn't possible, you have a big problem with the architecture of your code.
Also, make sure you stick non-executing JS at the bottom of the page near the </body> tag. This will allow everything else to load first, so that the bulky JS libraries don't slow down things like CSS and images.
The best practices way to deal with external javascript is to have it load after everything else on the page by putting it at the bottom of the page. Then everything that can be rendered will be and display and then the javascript at the bottom of the page will load and be compiled and cached. Of course this only works if the javascipt is a library of functions that don't need to be executed mid-page, in that case, you are stuck with serial javascript loading compiling and execution regardless.
Require.JS is a great library for automatically managing when your javascript loads.
You could load the file using the XMLHttpRequest object in JavaScript. (Aka AJAX). (end then of course just discard the result ^^).
I'm working on a website that currently has the same block of external JavaScript references on each page (added via a master page). I'd like to go through the site and identify which script files are actually necessary for each specific page to improve performance. Problem is there are a lot of pages and I'm not sure how to do it without resorting to manual trial and error process.
Is there a Firefox plug-in or some other tool that will identify which JavaScript references could actually be called by the page and which are not necessary?
you can use firebug plus his plugin for javascript code coverage: http://getfirebug.com/extensions/index.html#firebugcodecoverage