admin管理员组文章数量:1415073
Right now I am trying to get the following code to run, but the console never logs 'added', only 'started'.
var addWebsiteAddress;
console.log('started');
document.addEventListener('DOMContentLoaded', function(){
console.log('added');
addWebsiteAddress = document.forms["addWebsiteAddress"];
addWebsiteAddress.addEventListener('submit', addWebsite);
I've placed it in the HTML file as follows:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="popup.js" async></script>
<title>Add a website to block</title>
</head>
<body>
<form id="addWebsiteForm">
Website Address: <input type="text" id="websiteAddress"><br>
<input type="submit" value="Add Website">
</form>
</body>
</html>
What am I missing? DOMContentLoaded never seems to fire as I am unable to register the submit event as well.
Right now I am trying to get the following code to run, but the console never logs 'added', only 'started'.
var addWebsiteAddress;
console.log('started');
document.addEventListener('DOMContentLoaded', function(){
console.log('added');
addWebsiteAddress = document.forms["addWebsiteAddress"];
addWebsiteAddress.addEventListener('submit', addWebsite);
I've placed it in the HTML file as follows:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="popup.js" async></script>
<title>Add a website to block</title>
</head>
<body>
<form id="addWebsiteForm">
Website Address: <input type="text" id="websiteAddress"><br>
<input type="submit" value="Add Website">
</form>
</body>
</html>
What am I missing? DOMContentLoaded never seems to fire as I am unable to register the submit event as well.
Share Improve this question asked Jul 18, 2014 at 20:08 Eric GaoEric Gao 3,5382 gold badges20 silver badges30 bronze badges 2-
What happens if you take out
async
? – Xan Commented Jul 18, 2014 at 20:18 - Oh, it works. I don't understand why though - I thought async would just run the script separately from the loading of the web page. – Eric Gao Commented Jul 18, 2014 at 20:28
2 Answers
Reset to default 4The culprit here is async
attribute you're using.
Normally, when adding the <script>
tag to the DOM, the browser will pause parsing the page to execute the script. With async
, this is not guaranteed to be the case. It can be executed at an arbitrary point.
Your page is very small. Browser parses it nearly instantly and the event is fired before it "remembers" to execute your script - and as such it's not received by your script.
The idea behind async
is not to stumble with the page loading as the script itself is loading. Since the resources in a Chrome extension are all local, loading times are not a factor, and as such you should not use it, opting for a more predictable program flow.
Maybe you set "run_at": "document_end" in manifest.json. Change "run_at": "document_end" to "run_at": "document_start". It works! I tried!
本文标签: javascriptChrome Extension DOMContentLoaded Not FiringStack Overflow
版权声明:本文标题:javascript - Chrome Extension DOMContentLoaded Not Firing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745231225a2648832.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论