简体   繁体   中英

How do I include jQuery in my js file without html

I made a chrome extension where my popup button calls a script. The other script uses jQuery but I get an error saying jQuery is not defined.

My popup.html:

<!DOCTYPE html>
<html>
    <head>
        <title>HomAttendance</title>
    </head>
    <body>
        <h1 style="color:#E54E4E">Hom<span style="color:#4E97E5">Attendance</span></h1>
        <button type id="record" style="background-color:White"><h1 style="color:Black">Record Attendance</h1></button>
    </body>
    <script src="jquery-3.4.1.min.js"></script>
    <script src="popup.js"></script>
</html>

My popup.js:

document.addEventListener('DOMContentLoaded', function() {
  var login = document.getElementById('record');
  login.addEventListener('click', function() {
    chrome.tabs.executeScript({file: 'markStudents.js'});
  });
});

myScript.js:

var arrays = []
$.get('Attendance.txt', function(data){

    var splitted = data.split("\n"); // --> should return an array for each line 

    // Loop through all lines 
    for(var i = 0; i < splitted.length; i++) 
    {

        var line = splitted[i]; 

        // Now you could remove [ and ] from string  
        var removed = line.replace('[','').replace(']','');
        var refined = removed.replace(' ', '');

        // Now you can split all values by using , delimiter
        var values = refined.split(',');  
        var array = [];
        // Now you can iterate through all values and add them to your array
        for(var c = 0; c < values.length; c++) 
        {
            var value = values[c]; 
            array.push(value);
        }
        arrays.push(array);
    }
});
var present = arrays[0];
console.log(present);
var absent = arrays[1];
console.log(absent);

var user = present[0];
var pass = absent[0];
var loginField = document.getElementById('fieldAccount');
var passwordField = document.getElementById('fieldPassword');
loginField.value = user;
passwordField.value = pass;
var loginForm = document.getElementById('btn-enter-sign-in');

Is there any way to include my jquery.js in myScript.js?

Console Error

Just import jquery before you import popup.js

Like this

<!DOCTYPE html>
<html>
    <head>
        <title>HomAttendance</title>
    </head>
    <body>
        <h1 style="color:#E54E4E">Hom<span style="color:#4E97E5">Attendance</span></h1>
        <button type id="record" style="background-color:White"><h1 style="color:Black">Record Attendance</h1></button>
    </body>
    <script src="jquery-3.4.1.min.js"></script>
    <script src="popup.js"></script>
</html>

Inside Your popup.js, when you load markStudents.js which uses jQuery, you'd again have to load jQuery before same

Like this

document.addEventListener('DOMContentLoaded', function () {
    var login = document.getElementById('record');
    login.addEventListener('click', function () {
        chrome.tabs.executeScript(null, { file: "jquery-3.4.1.min.js" }, function () {
            chrome.tabs.executeScript(null, { file: "markStudents.js" });
        });
    });
});

Just reorder your script tags and put jQuery before your popup.js. That way it will be loaded when you try to call it.

yo can use this code to include another jquery file in your jquery:

$.getScript("file address");

like this:

$.getScript("/assets/pages/scripts/ui-blockui.min.js");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM