Javascript Cookie onload 调用
我有一个使用 javascript 做的 cookie,为了检查是否必须替换它,我在 HTML 的 body 标记中使用了“onload=checkCookie()”,但是现在我尝试将 js 放在另一个文件中,然后使用标签链接它,但如果我这样做,我就不能再仅使用“onload”调用这个函数,所以我想知道如何做到这一点? 我问过的其他人都告诉我将代码留在那里,但我想将我的设计分层 =S
这是 js:
function getCookie(c_name){
if (document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1){
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function checkCookie(){
username=getCookie('username');
if(username!=null && username!=""){
alert('Welcome again '+username+'!');
}else{
username=prompt('Please enter your name:',"");
if(username!=null && username!=""){
setCookie('username',username,365);
}
}
}
一切都在名为 init.js 的文件内
I have a cookie that I did using javascript and to check if it had to be replaced I used "onload=checkCookie()" in the body tag of the HTML, however now I'm trying to place the js in another file and just link it using the tag, but if I do that I can no longer call this function using just "onload" so I was wondering how can I do this? everybody else I've asked tells me to leave the code there, but I want to separate my design in layers =S
here's the js:
function getCookie(c_name){
if (document.cookie.length>0){
c_start=document.cookie.indexOf(c_name + "=");
if (c_start!=-1){
c_start=c_start + c_name.length+1;
c_end=document.cookie.indexOf(";",c_start);
if (c_end==-1) c_end=document.cookie.length;
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
}
function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}
function checkCookie(){
username=getCookie('username');
if(username!=null && username!=""){
alert('Welcome again '+username+'!');
}else{
username=prompt('Please enter your name:',"");
if(username!=null && username!=""){
setCookie('username',username,365);
}
}
}
Everything is inside a file called init.js
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不明白这个问题,你应该能够使用主体的 onload 来执行 javascript,即使它来自外部文件,如下所示:
这是 html:
这是链接的 javascript:
I don't understand the issue, you should be able to use the body's onload to execute javascript even if it is from an external file, like this:
This is the html:
This is the linked javascript:
将脚本放在单独的文件中是一件好事。
在脚本中添加onload函数 -
window.onload=checkCookie; //(无括号)并将其从 html 中取出。
Getting the script in a separate file is a good thing.
Add the onload function to the script-
window.onload=checkCookie; //(no parenthesis) and take it out of the html.