Bug Tracker

Opened 11 years ago

Closed 8 years ago

#10495 closed bug (migrated)

before() fails but native insertBefore() works ok

Reported by: pferreir Owned by: pferreir
Priority: low Milestone: None
Component: ajax Version: git
Keywords: Cc: jaubourg
Blocked by: Blocking:


While trying to inject the Facebook "like button" JS in a page in execution time, I got some funny behavior:

var fjs = $('script:first');
$('<script/>').attr({'async': true, 'src': '//connect.facebook.net/en_US/all.js#xfbml=1&appId=xxx'}).insertBefore(fjs)

fails (same with the equivalent before() call).


var fjs = $('script:first').get(0);
var e = $('<script/>').attr({'async': true, 'src': '//connect.facebook.net/en_US/all.js#xfbml=1&appId=xxx'}).get(0);
fjs.parentNode.insertBefore(e, fjs)

works just ok.

I tried different combinations of native/jQuery elements and they all fail. This happens at least since 1.6.1.

Change History (8)

comment:1 Changed 11 years ago by pferreir

I forgot to add that in the first case the element seems to be added to the DOM only to be immediately removed.

comment:2 Changed 11 years ago by dmethvin

Owner: set to pferreir
Status: newpending

Why not use $.getScript ? http://api.jquery.com/jQuery.getScript/

Can you provide a complete test case on jsfiddle.net? That would help us evaluate the problem.

comment:3 Changed 11 years ago by pferreir

Status: pendingnew


Here is a small example: http://jsfiddle.net/RQVsa/

The uncommented code should work (show the "Like" button), while the commented on should fail. I tried $.getScript, with no success...

comment:4 Changed 11 years ago by timmywil

Cc: jaubourg added
Component: unfiledajax
Priority: undecidedlow

We filter out scripts in manipulation. getScript should be used, but we remove hashes from the url. I'm not sure why, but it's been there a while.

comment:5 Changed 11 years ago by dmethvin

Status: newopen

Seems valid enough to keep open, until we figure out why we're removing the hash.

comment:6 Changed 10 years ago by dmethvin

Okay, this gives me an error from Facebook because of the xxx id:


This gives the same error and doesn't have the hash as timmywil said:


Since the script hasn't yet been executed the .insertBefore() is making the ajax request to request the script, but since the hash is stripped it's never going to see the correct id.

@jaubourg, do you know why we're stripping off the hash? Is there a specific reason for it, or just an omission?

comment:7 Changed 10 years ago by gibson042

A trip through the wayback machine reveals that we've been stripping hashes since 1.4.2's fix for #4987.

I'd be in favor of preserving them, but if #3808 kills it for oldIE (and thus 1.9) then we may be unable to while maintaining API symmetry.

comment:8 Changed 8 years ago by m_gol

Resolution: migrated
Status: openclosed
Note: See TracTickets for help on using tickets.