2016-03-20 26 views
-5

JavaScriptコードが動作しません。私が単に<script>alert("alert");</script>を自分のコードに入れたとしても、それは正常に機能しますが、次のコードのスクリプトの大部分は一見無視されます。このコードは、使用がリンクをクリックしたときに隠しフォームを送信するためのものです。これは単純な欠落中括弧ですか、それとももっと難しいものですか?JavaScriptコードが実行されません

コードは次のとおりです。

<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> 
+2

エラーがブラウザのコンソールに表示されますか? – nnnnnn

+0

私はそうは思わない。 –

+0

最後の機能には中かっこがありません。コードプロパティをインデントします。 – Biffen

答えて

2

このセクション

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; 
    } 
    } 

上記のコメントに記載されているように、実際にコードによってコンソールエラーが発生しました。それらを最初に確認して、何かがうまくいかない理由をすばやく見つけます。

+0

申し訳ありませんが、私はモバイルサファリを使用してこれを書き込もうとしています。デスクトップのサファリでも要素の検査機能が不足しているので、私は本当に正しいツールを持っていません。 –

+0

デスクトップSafariには本当に開発ツールがありますが、デフォルトでは有効になっていません。 Safariの設定を有効にするには、Safariの環境設定を開き、[詳細設定]をクリックして、[メニューバーに現像メニューを表示]チェックボックスをオンにします。 – ellitt

2

あなたはあなたの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()」で、いくつかのブラケットが含まれていません。

function onsubmit() { 
    alert('on submit'); 

    /* ')' was missing at the end of the if-clause! */ 
    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; 
    } 
} // <- '}' was missing here! 
+0

すみません。私はjavascriptコンソールを持っていない –

0

最後の「if」文に終了パラメータがありません。以下のフォロー:

あなたのコード

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

エラー:new Date(getCookie('expiry') < ---閉じパラメータが不足して ")"

修正されたコード:

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

ここで働いコード:http://codepen.io/theConstructor/pen/EKWvyJ?editors=1010

これが役立つことを願っています。

0

エラーを特定するためにコードをデバッグすることをお勧めします。 私は、エラーが見つかりましたが、それは一つだけだ場合、私は知らない。

function load() { 

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

を、それは要素の要素ではありませんので、それはgetElementsByNameだとも一つだけがあるので、もしあなたがターゲットとしているそれらのどの要素を選択する必要があります同じ名前の要素を使用すると、次のように呼び出すことができます。

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