Forum Moderators: open
I copied this code from somewhere ages back - does what I need it to do but I would like the form to submit using enter ...
------------JS--------------
<script language="JavaScript" type="text/JavaScript">
function loadpage(target){
document.location.href=document.frm.pswd.value + ".html"
}
</script>
-----------html-------------
<form name="frm">
Client Login
<input type="password" name="pswd" size="20">
<input name="button" type="button" onsubmit="loadpage(target)" onClick="loadpage(target)" value="Proceed">
</form>
----------------------------
...any clues? All the documentaton I can find abot this relates to method="" attribute but I have no clues how to implement it without a lot of trial and error (no time)
Cheers, Limbo
function loadpage(form){
window.location.href=form.pswd.value + ".html";
[blue]/* or, to remove current page from history..
window.location.replace(form.pswd.value + ".html");[/blue]
*/
}
HTML:
Changed
- button to type, submit
This will ensure that the form's onsubmit event is fired when enter is clicked, and the textbox is in focus.
- onsubmit handler goes on FORM element
- send reference to form with
this keyword. [pre]<form [color=brown]onsubmit="loadpage(this);return false"[/color]>
Client Login
<input type="password" name="pswd" size="20">
<input name="button" type="[color=brown]submit[/color]" value="Proceed">
</form>[/pre]
The point with the current script is that the password is the base filename of the target page. This is how the password is protected. The password page itself knows nothing, it just tries whatever is entered.
There are a couple of ways to approach this:
# say the password is: 'hello'
1. Dummy Image
Have an image called hello.jpg in the directory. First, we try to load the image by the same means. We have callback functions for onerror, and onload for the image, these functions can either load the page, or launch the alert.
This is fine in IE, but I have had trouble in other browsers. onerror is troublesome. I believe I had it working once, but don't know where the code is.
2. Hashed Password
I've never actually tried this.
There are a good few hashing scripts available out there. We keep the hashed password on the page. When we get the textbox entry, we hash the value, and if it matches the stored hash, then we load the page as previously, if not, then launch an alert.
So onto the next (inevitable) question. I have found the method you provided was great - however I'm back at square one if I use it, as the form does not submit with enter, and cook me a rabbit, I have no idea how to to get this to work now:
------------------JS
var checkPass_path = "pass/"
var checkPass_ext = ".html"
var checkPass_message = "Incorrect Password"
function checkPass(form)
{
var box = form.elements[0]
var input = box.value
if(input=="") doError()
var testim = new Image()
testim.onload = function()
{
window.location.href = checkPass_path + input + checkPass_ext
}
testim.onerror = doError
testim.src = checkPass_path + input + ".gif"
function doError(){ alert(checkPass_message); }
}
</script>
----------------html
<form>
<input type="password">
<input type="button" onclick="checkPass(this.form)" value="Proceed">
</form>
----------------
Ta, Limbo