Skip to content

Commit 77f6b42

Browse files
Added an extra reference implementation for $.getScript
1 parent 8b4a9b2 commit 77f6b42

3 files changed

Lines changed: 25 additions & 16 deletions

File tree

docs/migration_guide.md

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -106,22 +106,7 @@ Cash can parse `<script>` tags and execute their code when they are attached to
106106

107107
However we do not support script tags inside iframes or script tags with a `src` attribute.
108108

109-
If you need to load arbitrary JavaScript files you could use something like this:
110-
111-
```javascript
112-
function loadScript ( url ) {
113-
return new Promise ( ( resolve, reject ) => {
114-
const script = document.createElement ( 'script' );
115-
const head = document.getElementsByTagName ( 'head' )[0];
116-
const anchor = document.getElementsByTagName ( 'script' )[0];
117-
script.async = true;
118-
script.onload = resolve;
119-
script.onerror = reject;
120-
script.src = url;
121-
head.insertBefore ( script, anchor );
122-
});
123-
}
124-
```
109+
If you need to load arbitrary JavaScript files you can find a reference implementation for `$.getScript` [here](https://github.com/kenwheeler/cash/blob/master/src/extra/get_script.ts).
125110

126111
### No CSS auto-suffixing support for `zoom`
127112

pacco.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"browsers": ["ie >= 10"],
33
"components": {
44
"core/export_esm": false,
5+
"extra/get_script": false,
56
"extra/shorthands": false
67
},
78
"paths": {

src/extra/get_script.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
// @require core/cash.ts
3+
// @require core/variables.ts
4+
5+
function getScript ( url: string, success?: Function ): void {
6+
7+
const script = doc.createElement ( 'script' ),
8+
$anchor = cash ( 'script' );
9+
10+
script.async = true;
11+
script.src = url;
12+
13+
if ( success ) script.onload = () => success ();
14+
15+
$anchor.before ( script );
16+
17+
}
18+
19+
interface CashStatic {
20+
getScript ( url: string, success?: Function ): void;
21+
}
22+
23+
cash.getScript = getScript;

0 commit comments

Comments
 (0)