script src attribute
The src attribute on the script element:
- → allows you to link an external script file to the page (imported ⁄ external scripts)
- → the value of the src attribute represents the path = URL(absolute or relative) = address of that external script
- is a specific attribute → can be applied ONLY on the next html elements: audio, embed, iframe, img, input, script, source, track,video
!Observations
- src attribute is required only for imported ⁄ external scripts scripts that link to the page
- src attribute must not be present on inline scripts or data blocks ⁄islands
- if src attribute is present, see children allowed
- along with src, the next attributes can also be specified: charset, defer, async, crossorigin, type
Examples
< | attribute | = | "value(s)" | > | ... | </ | >Examples |
---|---|---|---|---|---|---|---|
6. | src | = | URL | <src="file.js"> </ > // imported javascript <src="file.js" type="application/javascript "> </ > // imported javascript <src="http://domain.com/file.js" crossorigin=""> </ > <src="file.js" charset="UTF-8" async defer> </ > |
Video demonstration script src attribute
HTML5 script src attribute tutorial
min | video details |
---|---|
00:04 | src attribute on script element allows you to link an external script file to the page |
00:17 | the HTML5 file |
00:19 | the external script file |
00:23 | this script statement will be executed only after we will link file.js to index.html |
00:32 | adding the correct syntax to page |
00:43 | the value of the src attribute represents the path=URL (absolute or relative)=address of the external script |
00:53 | the external script file file.js is now linked to index.html file |
01:00 | external script executed |
01:04 | linking file2.js (located in folder1) to index.html |
01:23 | file2.js correct path: folder1⁄file2.js |
01:33 | external script1 executed |
01:41 | if the path is wrong, the external script file is not linked to the page |
01:48 | only the 1st script is executed |
01:56 | Children restrictions: |
02:08 | if src attribute present: allowed empty space, new lines or comments only |
02:28 | valid content within the script element |
02:38 | invalid content within the script element: - script statements not allowed when scr attribute is present |