[英]How can I access variables from a JavaScript file from within a script I wrote in an HTML file?
So, I'm making a super basic (and also not secure, because I'm just playing around right now) login system that displays different values for premade graphs based on who you are. 因此,我正在制作一个超级基本的登录系统(并且也不安全,因为我现在正在玩),该登录系统会根据您的身份为预制图形显示不同的值。 I'm accessing the data for the graphs from a JSON with an array of objects that contain things like username, password, number of students, number of students that are boys, girls, etc. I want to have an ID variable that is set in my login page when the person logs in with credentials that match one of the objects' username and password in my JSON file.
我正在使用包含对象名称,密码,学生数量,男孩,女孩等学生数量的对象的数组从JSON访问图的数据。我想设置一个ID变量当用户使用与我的JSON文件中对象的用户名和密码之一匹配的凭据登录时,在我的登录页面中。 (the ID would be the index of whatever object the credentials matched).
(ID将是凭据匹配的任何对象的索引)。 When I make the graphs in my HTML file using chart js, I'm opening the JSON file and accessing one of its objects by that ID and accessing its contents that way.
当我使用图表js在HTML文件中创建图形时,我正在打开JSON文件并通过该ID访问其对象之一,并以这种方式访问其内容。 I'm struggling to find a way to reference this ID that is in my JavaScript file in my HTML code "scriptlet", if that's what it is called.
我正在设法找到一种方法来引用我的HTML代码“ scriptlet”中的JavaScript文件中的ID(如果是这样的话)。 Also, I DID try to include the JavaScript file in my html file, but I keep getting errors when I try to reference it in the html how I have in my snippet.
另外,我尝试将JavaScript文件包含在html文件中,但是当尝试在html中引用代码片段时,却不断出现错误。 Here's a sample of my code:
这是我的代码示例:
var objectID; //this is the ID I'm talking about in my question function clickedLoginButton(){ var script = document.createElement('script'); script.src = 'jquery-2.1.4.min.js'; script.type = 'text/javascript'; document.getElementsByTagName('head')[0].appendChild(script); var user = document.getElementById('username'); //inputted username var pass = document.getElementById('password'); //inputted password var passed = false; $.getJSON('data.json', function(data){ console.log("Length: " + data.length) i = 0; do{ console.log(data[i].login); if(user.value == data[i].login && pass.value == data[i].pass){ objectID = i; window.open("http://localhost/loggedIn.html", "_self", true); console.log("pass"); return; } i++; } while(i != data.length); if(passed == false){ window.open("http://localhost/loginFailed.html", "_self", true); objectID = 999999999; console.log("failed"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> //This is where I want to use the object ID var numPlayed; var numLazy; $.getJSON('data.json', function(data){ var chartData = [ { value: data[objectID].numStudents-data[objectID].numStudentsPlayed, color: "dimgray", highlight: "gray", label: "lazy" }, { value: data[objectID].numStudentsPlayed, color: "skyblue", highlight: "powderblue", label: "played" } ]; var ctx = document.getElementById("pieCanvas").getContext("2d"); var piechart = new Chart(ctx).Pie(chartData); }); </script>
I figured it out! 我想到了! You have to use cookies to access the ID for the user, and here's a tutorial for that:
您必须使用cookie来访问用户的ID,这是有关此的教程:
http://www.w3schools.com/js/js_cookies.asp http://www.w3schools.com/js/js_cookies.asp
The problem is that once you set the objectID
variable, you navigate away from the page. 问题在于,一旦设置了
objectID
变量,便会离开页面。 Once you navigated away from the page, you can no longer access JS variables defined in the previous page. 离开页面后,您将无法再访问上一页中定义的JS变量。
To go around this problem, you can store the value of the objectID
so that the next page can access it again. 要解决此问题,您可以存储
objectID
的值,以便下一页可以再次访问它。 You can do this using several methods. 您可以使用多种方法执行此操作。
Using query parameters of the next page. 使用下一页的查询参数。
When redirecting to the logged in page, you can specify the objectID. 重定向到登录页面时,可以指定objectID。
window.open("http://localhost/loggedIn.html?objectId="+objectID, "_self", true);
Then, in the script in the loggedIn page, you can access the objectID using window.location.search
然后,在loginIn页面的脚本中,您可以使用
window.location.search
访问objectID。
console.log(window.location.search); // outputs ?objectId=2343
Using localStorage 使用localStorage
You can use localStorage to store the value of objectID. 您可以使用localStorage来存储objectID的值。
localStorage.setItem('objectID', objectID)
then extract it in the logged in page using localStorage.getItem('objectID')
. localStorage.setItem('objectID', objectID)
然后使用localStorage.getItem('objectID')
将其提取到登录页面中。
Using Cookies 使用Cookies
As you mentioned above, you can use cookies to store and extract the value of objectID. 如上所述,您可以使用cookie来存储和提取objectID的值。 The disadvantage of this is that it is sent in all subsequent requests to the server.
这样做的缺点是,它将在所有后续请求中将其发送到服务器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.