UPDATE:
The problem is caused by including an old version of jQuery(1.4.2).
After I use 1.8.2 instead, the problem is just gone. But I still don’t
know why.
I recently made a Chrome extension. Thanks to the post chrome extension insert content script on browser action I realized that I have to include a js file like:
<script src="popup.js"></script>
But later I figure out it seems you cannot include more than one file:
<html>
<head>
<script src="jquery.js"></script> <!-- NOT LOADED -->
<script src="popup.js"></script> <!-- LOADED -->
</head>
<body>
</body>
</html>
What’s worse for me is that I cannot access the DOM of the popup HTML in the JS file. Like:
popup.html
<html>
<head>
<script src="popup.js"></script>
</head>
<body>
<a id="intro" href="#" target="_blank">Intro</a>
</body>
</html>
popup.js
/*
... jQuery Codes here ...
jquery.js cannot be included via src="jquery.js"
but by copy and paste its source code here it works
*/
$("#intro").click(function(){
alert("clicked"); // Not fireing at all
});
I am wondering what I can do to fix this. Thanks very much!
Here is a basic example of what you could use to get it working. My best guess is that it only appeared that jQuery wasn’t loading because of how your
popup.jsis set up (it is trying to attach theclickfunctionality before the element has loaded). I’m no JS expert, so I’m sure others will have better ideas of how to wait for theDOMto load, but this works as a basic test (NOTE: this uses a local version of the latest version of jQuery (1.8.2 currently), as the new content policies in manifest 2 do not allow in-line scripts):Manifest.json
Popup.html
Popup.js