1

I'm trying to add a script (like below) to the head but it's not working. Any ideas on how I'm messing this up?

$("<script src='http://mysite.com/slides.min.jquery.js'></script>").appendTo("head");
3
  • Why are you trying to add the jQuery script to the head, using jQuery? Commented May 6, 2011 at 10:59
  • sorry bad example. i edited the post. Commented May 6, 2011 at 11:00
  • why head? On runtime this is irrelevant for functionality. Commented May 6, 2011 at 11:02

4 Answers 4

2

you can use jQuery.getScript()

$.getScript('ajax/test.js', function() {
     alert('Load was performed.');
});
Sign up to request clarification or add additional context in comments.

Comments

1

For $ to work you need to have jquery loaded. So if it's not loaded yet, this won't work.

2 Comments

jQuery is actually loaded already in the footer. I'm using a platform that doesn't allow editing of the source code and I want to add another instance of jQuery to the head.
if jQuery is loaded, you can load other script using getScript() api.jquery.com/jQuery.getScript
1

Try something like that :

var head = $("head")
$("<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js' </script>").appendTo(head);

I won't ask you why you would like to import JQuery library since you already have JQuery in your page (you wouldn't use $ if that wasn't the case, don't you ;-) )

Comments

0

You don't have to append this to head. You can append it to body, it will work, no reason to worry about where it is appended to.

Comments

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.