How can I select the element of HTML and get its data attribute without using jquery?
For instance, I have this html tag with some data attribute,
<html xmlns="http://www.w3.org/1999/xhtml" class="no-js" data-directory='{"base_url":"http://localhost/mywebsite/","core_package":"base/","local_package":"base/"}'>
This is a jquery way,
var directories = $('html').data('directory');
What about javascript's native way?
Also, how can I select the script tag and get its attribute data?
For instance, I have this script tag,
> <script
> data-main="http://localhost/mywebsite/local/view/javascript/base/main"
> src="http://localhost/mywebsite/core/view/javascript/base/ext/require/require.js"></script>
a jquery way,
var base_url = $('script[src$="require.js"]').attr('src').split(/\b(?:core|local)\b/)[0];
But I need a javascript native way. Is it possible?
Using getElementsByTagName
1. .getAttribute("data-directories")
2. .src or .getAttribute("src") works the same
For newer browsers document.querySelector can be used like jQuery selectors which is of course smarter if there are more than one script on the page