Skip to main content

Bug Tracker

Side navigation

#10495 closed bug (migrated)

Opened October 14, 2011 02:35PM UTC

Closed October 20, 2014 11:46PM UTC

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:
Description

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

#!js
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).

While

#!js
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.

Attachments (0)
Change History (8)

Changed October 14, 2011 02:36PM UTC by pferreir comment:1

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

Changed October 14, 2011 03:05PM UTC by dmethvin comment:2

owner: → 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.

Changed October 17, 2011 10:11AM UTC by pferreir comment:3

status: pendingnew

Hello,

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...

Changed October 19, 2011 05:58PM UTC by timmywil comment:4

cc: → jaubourg
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.

Changed October 20, 2011 09:16PM UTC by dmethvin comment:5

status: newopen

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

Changed January 06, 2013 09:14PM UTC by dmethvin comment:6

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

http://jsfiddle.net/RQVsa/10/

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

http://jsfiddle.net/RQVsa/11/

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?

Changed January 08, 2013 03:48PM UTC by gibson042 comment:7

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.

Changed October 20, 2014 11:46PM UTC by m_gol comment:8

resolution: → migrated
status: openclosed