This is my HTML:
<!doctype html>
<html>
<head>
<title>PassVault</title>
<link rel="stylesheet" type="text/css" href="stil.css">
<meta charset="utf-8">
<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js'></script>
<script type="text/javascript">
$(document).ready(function () {
$("div").css("border", "3px solid red");
});
</script>
</head>
<body>
<div id="rainbow"> </div>
<div id="loginBox">
<div id="welcome"> Dobrodošli, uporabnik! </div><br>
</div>
</body>
</html>
After the document loads up, it's supposed to put a red border around all my divs (there's plenty in the HTML file), but it doesn't. I don't see what's wrong here. The jQuery is all in the same file and the link where jQuery is hosted is provided by Google and also works.
It's worth mentioning that the .html file is called by browser_action
from the manifest.json file of a Google Chrome extension. So, it opens the popup this way:
It is also worth mentioning that the above image is just an example image provided by Google. This is not my image. My div's have no borders and the jQuery doesn't change that.
manifest.json
....
"browser_action": {
"default_popup": "popupindex.html",
}
....
I don't see how this window being a popup would affect the .js file functionality but I'm sure it has something to do with that.
Added from comment on an answer by OP:
Adding a border around all these divs was just my way of testing whether or not jQuery works. I will be needing jQuery for plenty of other things in the future. It doesn't even work with this simple thing.
Try moving your
script
tag to the bottom of the body, when all the elements have loaded.You are attempting to load/run scripts that violate the Content Security Policy. This affects both your attempt to load jQuery from a source external to your extension and your attempted use of an inline script (your
$(document).read()
code).You can access the console for the popup by right-clicking in the popup and selecting "Inspect". The console would have shown you the following errors:
and
Extension Default Content Security Policy
For Chrome extensions, the default Content Security Policy is:
Google explains that the reasons for this are:
Loading jQuery
For loading jQuery, the best solution is to download the jQuery code. From the question, the code you are using is at:
http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
. However, as mentioned by Ted, that is quite an old version of jQuery. Unless you have a reason to be using an older version, you might tryhttps://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js
. You can then store that file in your extension directory (or in a subdirectory) and include it in your popupindex.html withYour own code
Your own JavaScript code is not running because the default content security policy for extensions does not permit inline scripts. The best solution is to move your
$(document).ready()
code into a separate file (e.g. popupindex.js) which is then included in your popupindex.html using:Obviously, that needs to be after the
<script>
tag that is loading jQuery.You can include inline scripts, but you will need to supply a "hash of the script in the "script-src" directive" in the value for the
content_security_policy
key within your manifest.json. However, doing so is just not worth the time and effort. It is much easier to move the code into a separate file.JavaScript included in HTML defined event handlers is also not permitted
Code that you add in HTML for event handlers is JavaScript and is also not permitted. For example, the following will fail:
You need to code that as:
Then, in your separate JavaScript file (see above), something like:
Complete extension with popup running jQuery
The following complete extension, which runs your jQuery code, produces a popup which looks like:
manifest.json:
popupindex.html:
popupindex.js:
jquery.min.js:
Downloaded from https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js and stored as jquery.min.js in the extension's directory.