Reputation: 12392
I am a javascript and chrome devtools rookie. We have a page with content like:
<head>
..
<script type="text/javascript" src=”path/to/some/external/script1.js>
..
</head>
<body>
… some HTML
<script type="text/javascript”>
..some inline javascript code here
</script>
...some more HTML
<script type="text/javascript”>
..some more inline javascript code here
</script>
</body>
My understanding is that the browser will execute the javascript in the order it appears on the page. When a JS is executed by the browser, can I have the browser print a message like "Now executed script.js" without modifying script.js
Upvotes: 2
Views: 169
Reputation: 1
Using MutationObserver, create the following inline script as the very first tag in <head>
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.type == 'childList') {
[ ].filter.call(mutation.addedNodes, function (node) {
return node.nodeName == 'SCRIPT' && node.src.length;
}).forEach(function (script) {
script.addEventListener('load', function () {
console.log('finished loading', script.src);
});
});
}
});
});
var config = { subtree: true, childList: true };
var target = document;
observer.observe(target, config);
Though, this will tell you when the load event triggers, I'm not sure how this relates to the script being run
Initial tests (in Firefox) suggest the load event fires after the code has run - your mileage may vary
And the usual Internet Exploder caveat - only works in IE11 - for IE9/IE10 you could do something with DOMSubtreeModified
event, though I'm pretty sure it would be painful to do so. for IE8 and earlier, I would recommend a derisive message telling the user to stop using rubbish browsers :p
Upvotes: 1
Reputation: 163518
The only thing you can do with this is put a <script>
tag right under it, and run code there.
Upvotes: 1