gpt4 book ai didi

Javascript Cookie onload 调用

转载 作者:行者123 更新时间:2023-11-30 13:43:50 25 4
gpt4 key购买 nike

我有一个我使用 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 的文件中

最佳答案

我不明白这个问题,即使它来自外部文件,你也应该能够使用主体的 onload 来执行 javascript,如下所示:

这是 html:

<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body onload="hello()">
</body>
</html>

这是链接的javascript:

var hello = function()
{
alert("Hello!");
}

关于Javascript Cookie onload 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/670384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com