javascript - Chrome Extension DOMContentLoaded Not Firing - Stack Overflow

Right now I am trying to get the following code to run, but the console never logs 'added', o

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
Add a ment  | 

2 Answers 2

Reset to default 4

The 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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745231225a4617686.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信