Skip to content

Assign attrs to created script element #3

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

Merged
merged 1 commit into from
Apr 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ loadScript('https://example.com/script.js')
})
```

**Additional attributes**

```js
var loadScript = require('tiny-script-loader/loadScript')
var loadScriptPromised = require('tiny-script-loader/loadScriptPromised')

loadScript('https://example.com/script.js', callbackFn, { crossOrigin: true })
loadScriptPromised('https://example.com/script.js', { type: 'javascript' })
.then(fn)
```
## License

[Apache-2.0](/LICENSE)
7 changes: 6 additions & 1 deletion loadScript.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
module.exports = function loadScript (src, cb) {
module.exports = function loadScript (src, cb, attrs) {
var doc = document
var tag = 'script'
var firstScript
var el
el = doc.createElement(tag)
firstScript = doc.getElementsByTagName(tag)[0]
if (attrs) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Placement is important here—we want to control & possibly overwrite async, src, onload, etc

Copy link

@mmase mmase Apr 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you make attrs = {} by default and then just simply (with no conditional):

Object.assign(el, attrs);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep in mind that we're not using babel here, so let's keep compatibility to IE11.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

carry on!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah im trying to keep it old school in this PR.

Object.keys(attrs).forEach(function (key) {
el[key] = attrs[key]
})
}
el.async = 1
el.src = src
el.onload = function () { cb() }
Expand Down
7 changes: 6 additions & 1 deletion loadScriptPromised.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module.exports = function loadScriptPromised (src) {
module.exports = function loadScriptPromised (src, attrs) {
var doc = document
var tag = 'script'
var firstScript
var el
return new Promise(function (resolve, reject) {
el = doc.createElement(tag)
firstScript = doc.getElementsByTagName(tag)[0]
if (attrs) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto for above

Object.keys(attrs).forEach(function (key) {
el[key] = attrs[key]
})
}
el.async = 1
el.src = src
el.onload = function () { resolve() }
Expand Down
Loading