Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xhook breaks jQuery's <script src> functionality #22

Closed
iameli opened this issue Aug 7, 2014 · 3 comments
Closed

xhook breaks jQuery's <script src> functionality #22

iameli opened this issue Aug 7, 2014 · 3 comments
Labels

Comments

@iameli
Copy link

iameli commented Aug 7, 2014

Hi!

It appears that xhook (and thus xdomain) messes with jQuery's ability to parse and execute <script> tags with a src parameter in dynamically-added markup. Here's a plunkr with reproduction: http://plnkr.co/edit/005F3745KFBQWgeFiZES?p=preview

(I'm including xdomain there but I repro'd with vanilla xhook as well so I figured that this was the right place to file a bug.)

The expected behavior here is that jQuery will parse out that <script> tag, request the script, and execute it, replacing the <h1> text with "hello, world". All of that happens if you comment out or remove xdomain.

I tried a variety of versions of jQuery, seemed consistent across them.

Thanks!

@iameli iameli changed the title xhook breaks jQuery's <script src> functionality xhook breaks jQuery's <script src> functionality Aug 7, 2014
@jpillora
Copy link
Owner

Hi @iameli Just debugged this. So jQuery is parsing the HTML and concluding that you're injecting a script, so in order for it to bind on load events, it translates that into a synchronous XHR+eval. XHook does not support synchronous XHR because I allow asynchronous hooks, I could detect if there are asynchronous hooks, though when it loads - XDomain creates an asynchronous hook, so this would still be broken in the majority of cases. I am throwing an error about this though jQuery is silently throwing it away :(

Since I can't support sync XHR I'll have to close this as "wont fix", though I can offer a workaround.

Instead of jQuery:

$('head').append('<script type="text/javascript" src="script.js">');

Vanilla

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "script.js";
document.querySelector("head").appendChild(script);

This is definitely worth including in the docs.

As a side note, since the world is moving away from IE67, I think people should try to depend less on jQuery. See here.

@iameli
Copy link
Author

iameli commented Aug 21, 2014

Interesting! TIL that synchronous XHR is a thing.

We're actually using xdomain as part of our project to move into a new API and away from our old jQuery based application, so we're definitely on the same page there. :) I ended up working around it in a very similar fashion -- the project was already including head.js for unrelated reasons so I just ended up using that.

Thanks for looking into it!

@jpillora
Copy link
Owner

jpillora commented Sep 6, 2014

Hey @iameli just an update to this, I've implemented sync requests in XHook to fix a chrome iOS bug so this should resolve some of these issues. Though note, when the request is sync, async hooks must be skipped and this includes XDomain so this just attempts to allow external code to operate correctly, we still can't use XDomain to allow sync crossdomain calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants