您可以将 id 添加到 script 标签,然后通过 id 从 DOM 中获取它:
document.write('<script type="text/javascript" src="/plugin.js" id="myscript"></script>');
if (document.getElementById('myscript')) {
console.info('success');
} else {
console.error('failure');
}
或者,如果您不想更改任何内容,可以尝试在页面上的所有脚本中找到它:
document.write('<script type="text/javascript" src="/plugin.js"></script>');
(function () {
var scripts = document.getElementsByTagName('script'),
l = scripts.length - 1;
// If you'll check that just after document.write then it should be last script
// in the other case you would need to check all scripts.
// From src I'm removing domain - at least Safari is returning full URL here
if (scripts.length && scripts[l - 1].src && scripts[l - 1].src.replace(/^([a-z]+:\/\/[^\/]*)/i, '') == '/plugin.js') {
console.info('success');
} else {
console.error('failure');
}
}());