Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript代码没有';不执行_Javascript_Html - Fatal编程技术网

Javascript代码没有';不执行

Javascript代码没有';不执行,javascript,html,Javascript,Html,我的javascript代码就是不起作用。如果我简单地把alert(“alert”)在我的代码中,它正常工作,但是下面代码中的大部分脚本似乎被忽略了。该代码用于在用户单击链接时提交隐藏表单。这是一个简单的缺少的花括号,还是更难的 守则如下: <html> <head> <script> function $_GET(q,s) { s = (s) ? s : window.location.search; var re = new RegExp(

我的javascript代码就是不起作用。如果我简单地把
alert(“alert”)
在我的代码中,它正常工作,但是下面代码中的大部分脚本似乎被忽略了。该代码用于在用户单击链接时提交隐藏表单。这是一个简单的缺少的花括号,还是更难的

守则如下:

<html>
<head>
<script>
function $_GET(q,s) {
    s = (s) ? s : window.location.search;
    var re = new RegExp('&amp;'+q+'=([^&amp;]*)','i');
    return (s=s.replace(/^\?/,'&amp;').match(re)) ?s=s[1] :s='';
}

function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays*24*60*60*1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1);
        if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
    }
    return "";
}

Date.prototype.addDays = function(days)
{
    var dat = new Date(this.valueOf());
    dat.setDate(dat.getDate() + days);
    return dat;
}

function stripGet(url) {
  return url.split("?")[0];
}

function load() {

alert('load');
document.getElementByName('link').onclick=onsubmit;

if($_GET('l') === "penguin"){
setCookie('expiry', new Date.addDays(30), 400);
setCookie('expired', 'FALSE', 30);
window.location = stripGet(window.location);
}
}

function onsubmit(){

alert('on submit');

if (new Date() > new Date(getCookie('expiry') || getCookie('expired') == 'TRUE') {
alert("expired");
setCookie('expired', 'TRUE', 1000);
window.location ='???';
return false;
}
else {
alert("valid");
document.getElementByName('username').value = atob('???');
document.getElementByName('username').value = atob('???');
document.forms['form'].submit();
return true;
}

</script>
</head>
<body onload="load()" bgcolor="green" top="45%" align="center" link="orange" active="blue" visited="orange">

<form name="form" action="submit.php" method="POST">
<input name="__formname__" type="hidden" value="loginform">
<input name="username" type="hidden">
<input name="username" type="hidden">
</form>

<a name="link" href="javascript:onsubmit();" onclick="alert("click"); onsubmit(); return true;">
<h1 style="font-size: 84pt; padding-top: 1.5cm">
Submit form
</h1>
</a>
</body>
</html>

函数$\u GET(q,s){
s=(s)?s:window.location.search;
var re=new RegExp('&;'+q+'=([^&;]*),'i');
返回(s=s.replace(/^\?/,“&;”).match(re))?s=s[1]:s='';
}
函数setCookie(cname、cvalue、exdays){
var d=新日期();
d、 设置时间(d.getTime()+(exdays*24*60*60*1000));
var expires=“expires=“+d.toutString();
document.cookie=cname+“=”+cvalue+”;“+expires;
}
函数getCookie(cname){
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);
对于(var i=0;i新日期(getCookie('expirement')| | getCookie('expired')=='TRUE')){
警报(“过期”);
setCookie('expired','TRUE',1000);
window.location='???';
返回false;
}
否则{
警报(“有效”);
document.getElementByName('username')。value=atob('???');
document.getElementByName('username')。value=atob('???');
document.forms['form'].submit();
返回true;
}
本节

if (new Date() > new Date(getCookie('expiry') || getCookie('expired') == 'TRUE') {
缺少if语句的右括号

整个函数需要一些语法工作,因此下面的内容应该可以解决它

function onsubmit() {

  alert('on submit');

  if (new Date() > new Date(getCookie('expiry') || getCookie('expired') == 'TRUE')) {
      alert("expired");
      setCookie('expired', 'TRUE', 1000);
      window.location = '???';
      return false;
    } else {
      alert("valid");
      document.getElementByName('username').value = atob('???');
      document.getElementByName('username').value = atob('???');
      document.forms['form'].submit();
      return true;
    }
  }

如上面的评论所述,确实存在由代码导致的控制台错误。请务必先检查这些错误,您将很快发现某些错误不起作用的原因。

您应该看看您的javascript控制台:

Uncaught SyntaxError: Unexpected token {
page.html:70 Uncaught ReferenceError: load is not defined
page.html:79 Uncaught SyntaxError: Unexpected token }
(program):1 Uncaught TypeError: onsubmit is not a function
您的代码在函数“onsubmit()”中缺少一些括号

函数onsubmit(){
警报(“提交时”);
/*if子句末尾缺少“)”*/
如果(new Date()>new Date(getCookie('expired')| | getCookie('expired')=='TRUE')){
警报(“过期”);
setCookie('expired','TRUE',1000);
window.location='???';
返回false;
}
否则{
警报(“有效”);
document.getElementByName('username')。value=atob('???');
document.getElementByName('username')。value=atob('???');
document.forms['form'].submit();
返回true;
}

}//您在最后一次“如果”陈述中缺少一个结束参数。如下所示:

你的代码

if (new Date() > new Date(getCookie('expiry') || getCookie('expired') == 'TRUE') {...}

错误:
newdate(getCookie('expiration')
最好调试代码以确定错误。 我发现了一个错误,但我不知道这是否是唯一的错误:

function load() {

alert('load');
document.getElementByName('link').onclick=onsubmit;
它是getElementsByName,所以它是元素而不是元素,并且您需要选择目标元素中的哪个元素,因此如果只有一个元素具有相同的名称,那么您可以这样调用它:

document.getElementsByName('link')[0].onclick=onsubmit;

您是否在浏览器控制台中报告了错误?最后一个函数似乎缺少一个花括号。缩进您的代码属性。调试帮助问题需要根据。您的代码不是最小的。请回答您的问题以确保您的代码是最小的(仅是在问题中重现您的问题所需的代码),完整(用户不需要任何其他东西来重现您的问题),并且可验证(提供的代码确实重现了您面临的确切问题)。因为你的问题与堆栈溢出无关。请注意,这也是一个常见的问题。抱歉。我认为它可能有用,但如果缺少几个括号,那么这个问题将被删除。这是不公平的。我发布了一个问题,它原来是一个垃圾问题,但我无法删除。这打击了我的声誉。是的,对不起fari确实有开发工具,只是默认情况下没有启用。如果您想打开它们,请打开Safari的首选项,单击“高级”,然后选中“在菜单栏中显示开发菜单”旁边的框。对不起,我没有javascript控制台
function load() {

alert('load');
document.getElementByName('link').onclick=onsubmit;
document.getElementsByName('link')[0].onclick=onsubmit;