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: |
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 (8)
comment:1 Changed 11 years ago by
comment:2 Changed 11 years ago by
Owner: | set to pferreir |
---|---|
Status: | new → 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 11 years ago by
Status: | pending → 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 11 years ago by
Cc: | jaubourg added |
---|---|
Component: | unfiled → ajax |
Priority: | undecided → low |
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
Status: | new → open |
---|
Seems valid enough to keep open, until we figure out why we're removing the hash.
comment:6 Changed 10 years ago by
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 10 years ago by
comment:8 Changed 8 years ago by
Resolution: | → migrated |
---|---|
Status: | open → closed |
Migrated to https://github.com/jquery/jquery/issues/1732
I forgot to add that in the first case the element seems to be added to the DOM only to be immediately removed.