Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 47efe44

Browse files
committed
fix($browser.addJs): make addJs jQuery compatible
Change addJs implementation to avoid use of jQuery because of issues that affect angular-ie-compat.js. See inlined comment for more info.
1 parent c52e749 commit 47efe44

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

src/Browser.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,26 @@ function Browser(window, document, body, XHR, $log) {
416416
* @methodOf angular.service.$browser
417417
*
418418
* @param {string} url Url to js file
419-
* @param {string=} dom_id Optional id for the script tag
419+
* @param {string=} domId Optional id for the script tag
420420
*
421421
* @description
422422
* Adds a script tag to the head.
423423
*/
424-
self.addJs = function(url, dom_id) {
425-
var script = jqLite(rawDocument.createElement('script'));
426-
script.attr('type', 'text/javascript');
427-
script.attr('src', url);
428-
if (dom_id) script.attr('id', dom_id);
429-
body.append(script);
424+
self.addJs = function(url, domId) {
425+
// we can't use jQuery/jqLite here because jQuery does crazy shit with script elements, e.g.:
426+
// - fetches local scripts via XHR and evals them
427+
// - adds and immediately removes script elements from the document
428+
//
429+
// We need addJs to be able to add angular-ie-compat.js which is very special and must remain
430+
// part of the DOM so that the embedded images can reference it. jQuery's append implementation
431+
// (v1.4.2) fubars it.
432+
var script = rawDocument.createElement('script');
433+
434+
script.type = 'text/javascript';
435+
script.src = url;
436+
if (domId) script.id = domId;
437+
body[0].appendChild(script);
438+
439+
return script;
430440
};
431441
}

test/BrowserSpecs.js

+24
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,28 @@ describe('browser', function(){
505505
});
506506
});
507507
});
508+
509+
describe('addJs', function() {
510+
511+
it('should append a script tag to body', function() {
512+
browser.addJs('http://localhost/bar.js');
513+
expect(scripts.length).toBe(1);
514+
expect(scripts[0].src).toBe('http://localhost/bar.js');
515+
expect(scripts[0].id).toBe('');
516+
});
517+
518+
519+
it('should append a script with an id to body', function() {
520+
browser.addJs('http://localhost/bar.js', 'foo-id');
521+
expect(scripts.length).toBe(1);
522+
expect(scripts[0].src).toBe('http://localhost/bar.js');
523+
expect(scripts[0].id).toBe('foo-id');
524+
});
525+
526+
527+
it('should return the appended script element', function() {
528+
var script = browser.addJs('http://localhost/bar.js');
529+
expect(script).toBe(scripts[0]);
530+
});
531+
});
508532
});

0 commit comments

Comments
 (0)