Bug Tracker

Modify

Ticket #10495 (open bug)

Opened 19 months ago

Last modified 4 months ago

before() fails but native insertBefore() works ok

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

Description

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

While

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

comment:1 Changed 19 months 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 19 months ago by dmethvin

  • Owner set to pferreir
  • Status changed from new to pending

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 19 months ago by pferreir

  • Status changed from pending to new

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

comment:4 Changed 19 months ago by timmywil

  • Cc jaubourg added
  • Priority changed from undecided to low
  • Component changed from unfiled to ajax

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 19 months ago by dmethvin

  • Status changed from new to open

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

comment:6 Changed 4 months ago by dmethvin

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?

comment:7 Changed 4 months 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.

Please follow the  bug reporting guidlines and use  jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

View

Add a comment

Modify Ticket

Action
as open
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.