0

I'm looking for a javascript solution to find all jQuery scripts on a html page and replace the code with the latest version of jQuery.

Original line:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

Replace with:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script>
6
  • replace it? can't you modify the code instead of changing the jQuery library with jQuery? Commented Dec 1, 2011 at 13:44
  • 3
    Beware, many things changed since 1.3. It may break, those versions aren't fully compatible Commented Dec 1, 2011 at 13:49
  • I understand how simple the solution can be. Unfortunately this isn't possible. I really looking for the javascript solution. Commented Dec 1, 2011 at 13:53
  • Isn't there a way to just point to the latest library? Commented Dec 1, 2011 at 13:54
  • I don't think that jquery.com is a CDN... (or at least effective in being one) Commented Dec 1, 2011 at 13:59

3 Answers 3

2

Most of the examples people are writing DON'T WORK! (Examples have been removed.) jQuery().jquery returns the jQuery version:

$("script[src='http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js']")
    .attr("src","http://code.jquery.com/jquery-1.7.1.js");
console.log(jQuery().jquery);

This writes 1.3.2 to the console. Just changing the src of a script doesn't make the new script actually run! Even if you give the new script chance to load with a setTimeout or load event, it still won't change the version.

You could load the script as an AJAX query and eval it.

jQuery.getScript("http://code.jquery.com/jquery-1.7.1.js");

But even that might not produce the result you expect. Any reference to jQuery or $ further down the page will have already pointed to the old jQuery object (version 1.3.2), not the new one. So to use 1.7.2, you'd then have to eval all your scripts only after getting and evaling the new script. So you're gonna have to find another way I'm afraid.

Edit: I suppose you could do this:

jQuery.getScript("http://code.jquery.com/jquery-1.7.1.js", function () {
    // Only after jQuery has run
    jQuery.getScript('my-script-which-depends-on-1.7.js');
});

but it's a bit cumbersome.

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

Comments

0
var all = document.querySelectorAll('script[src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"]'),
script;
for(var i=0, len = all.length; i < len; i++){
    script = all[i].cloneNode(false);
    script.src = "http://code.jquery.com/jquery-1.7.1.js";
    document.body.removeChild(all[i]);
    document.body.appendChild(script);
}

That works for me. You can see it in this demo, but you'll have to type the command $.fn.jquery in the console to see that the version has actually changed.

1 Comment

You can demonstrate that the source has changed all you want, but it won't actually run the loaded script.
-1

Open up the source of your project in an editor like notepad++ or any other advanced source code editor. Search for the text "1.3.2/jquery.min.js" in all available source files (by recursively searching through the root of your project). Check if all occurrences of the string need to be replaced and then use the find/replace tool to change it to the latest version.

2 Comments

He wants to do this using JavaScript. (Odd, but well, that's what he asked for.)
I understand how simple the solution can be. Unfortunately this isn't possible. I really looking for the javascript solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.