2015-07-10 56 views
-1

我試圖做到這一點,當我去到網站,有一個按鈕,當你按下它,它會提示你「密碼是什麼?」爲了確定它是否正確,我使用concat字符串來接收提示答案並添加.html到最後,並且我希望它重定向到(passwordtheyenter).html,因此如果他們輸入「test」作爲密碼,它「會帶他們去 「的test.html」爲什麼我的html/javascript代碼不工作?

這裏是我的代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <script> 
     function testFunction() { 
     var str1 = prompt.("What's the password?"); 
     var str2 = ".html"; 
     var pass = str1.concat(str2); 
     } 
    </script> 
</head> 
<body> 
<button onclick="testFunction()">Chat</button> 


<script> 
    open.window(pass) 
</script> 
</body> 
</html> 

感謝。

+1

什麼是'提示()'?您應該打開控制檯(大多數瀏覽器都是F12)。它會讓你知道你的代碼是否有錯誤。 –

+0

如果有人嘗試使用幾個密碼並找到您不希望他們看到的頁面,會發生什麼情況? – 2015-07-10 01:48:17

+0

你永遠不應該使用客戶端驗證作爲你的主要認證手段。 –

回答

2

有你的HTML/Javscript代碼不能正常工作的四個主要的原因。

首先,變量pass的範圍僅在函數中定義,因此只能在該函數內進行訪問。在函數之外定義它,然後在其中設置它的值。

編輯:因爲pass現在只被函數使用,所以不需要在函數之外定義它。

接下來,我相信你試圖使用window.open()函數。你在你的代碼中寫了open.window()。

第三,你有意外的提示經過一段時間的()調用

第四,你應該有window.open()函數中調用,以便它不運行,直到用實際點擊按鈕。

<!DOCTYPE html> 
<html> 
<head> 
    <script> 
     function testFunction() { 
      var str1 = prompt("What's the password?"); 
      var str2 = ".html"; 
      var pass = str1 + str2; 
      window.open(pass); 
     } 
    </script> 
</head> 
<body> 
<button onclick="testFunction()">Chat</button> 
</body> 
</html> 
+0

這對我來說,謝謝。 – Drew

+1

@Drew如果你在'testFunction'裏面調用'window.open',那麼你不需要在全局中設置'pass'變量 – jasonscript

0

pass變量只在testFunction內部定義,所以當您嘗試呼叫open.window時,您無法訪問它。

嘗試在testFunction返回pass和結果傳遞給open.window

+0

對不起,我對這類東西真的很陌生。你能否詳細說明如何做到這一點?我真的很感謝答案。 – Drew

+0

您的open.window函數在頁面加載時得到執行,但您只在用戶點擊按鈕時提示用戶。將open.window移動到testFunction中,並刪除按鈕後面的腳本標記。 –

+0

(如果這是你的回覆),這是我的新代碼,但它仍然無法工作:http://pastebin.com/ZFUfre0W – Drew

0

你可能在尋找:

function testFunction() { 
    // Prompt user for input 
    var str1 = window.prompt("What's the password?"); 
    // Append .html to user input 
    var str2 = ".html"; 
    // Open new window with concatenated result 
    window.open(str1.concat(str2)); 
} 

然後,您可以刪除:

<script> 
    open.window(pass) 
</script> 

這是工作:https://jsbin.com/gewudaruda/edit?html,output

0

嘗試更換的第一個腳本標籤與下面的一個。 建議使用標籤的'type'屬性來定義腳本的類型。是

<script type="text/javascript"> 
    function testFunction() { 
    var str1 = prompt("What's the password?"); 
    window.open(str1+".html"); 
    } 
</script> 
+0

在HTML5中,type屬性是可選的,默認爲'text/javascript'根據http://www.w3.org/TR/html5/scripting-1.html#attr-script-type – EasyCo

1

與您的代碼中存在的問題如下:

  1. prompt.()在語法上是無效的JavaScript代碼。它會給你一個錯誤,這可以在控制檯中看到(你可以在大多數瀏覽器中按F12打開它,或者在Opera中按CTRL + SHIFT + I)。

  2. open.window()是語義無效的Javascript代碼。這是沒有全球變量open。你可能想要的是window.open(...)

  3. 您的代碼寫入方式沒有多大意義。當按鈕被點擊時,該函數被調用,但它什麼也不做。底部的Javascript會給你一個錯誤,但即使它是有效的Javascript,它仍然會給你一個錯誤,因爲變量pass未在全局範圍中定義;它是在函數內部定義的。一旦函數運行,變量將被遺忘。

這將是更好的按鈕,而不是添加一個單擊處理程序:

<!doctype html> 

<!-- The button --> 
<button id="my-button">Chat</button> 

<script> 
    // Get the button 
    var myButton = document.getElementById("my-button"); 

    // Set a click handler on the button 
    myButton.onclick = function() { 

     // Get the page 
     var page = prompt("What's the password?"); 

     // Set a new location for the window; this sends the 
     // current page to the new page 
     window.location = page + ".html"; 
    }; 
</script>