0

I am trying to use jquery UI for slider in a bookmarklet. and jquery ui requires to include the file after normal jquery file.

So what I've tried so far was just appending the script to header while making sure that ui get added after normal jquery but this did not work i suspect it might be because i appended it and not pre pended it.

So i am now looking for ways to pre pend in Javascript after some time searching i've found :

var parent_head = document.getElementsByTagName("head")[0];
document.getElementsByTagName("head")[0].appendChild(jqueryUI, parent_head.firstChild);

This however seems to append it in a normal way.

Is there a better way?

Mike Samuel
121k30 gold badges230 silver badges255 bronze badges
asked Dec 29, 2012 at 22:39
2
  • How about replacing appendChild with insertBefore? Commented Dec 29, 2012 at 22:54
  • The position a script is inserted dynamically does not matter. Commented Dec 29, 2012 at 23:02

1 Answer 1

3

Use insertBefore with the first child as the insertion point.

parent_head.insertBefore(jqueryUI, parent_head.firstChild)

For example,

var list = document.createElement('ul');
document.body.appendChild(list);
list.innerHTML = "<li>First</li><li>Second</li>";
var newItem = document.createElement('li');
newItem.innerHTML = "Firster";
list.insertBefore(newItem, list.firstChild);

shows

  • Firster
  • First
  • Second
answered Dec 29, 2012 at 22:54
Sign up to request clarification or add additional context in comments.

1 Comment

yea that worked seems it wasn't the problem bah i guess I'll have to make my own sliders. thanks very much for help

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.