Simple Password Javascript Code? - javascript

I need a Javascript application that, when run, prompts a password to be entered, and if the password is correct, the script causes the webpage to close. If the password is incorrect, the script prompts for the password to be entered again.
I'm planning on loading this script onto my cell phone, which doesn't have a password-protected keylock feature.

Don't know if this works on your cell phone, but it does with my browser:
<head>
<script language="JavaScript">
var pass_entered;
var password="cool";
while (pass_entered!=password) {
pass_entered=prompt('Please enter the password:','');
}
self.close();
</script>
</head>

Javascript "keylock" on a cell phone will probably be trivial to work around.
Anyway, if you really want to check password in Javascript, you can at least avoid putting it in plain text in page source.
Get MD5 JS implementation, and compare (salted!) password hashes instead.

Ok, we can have two approuches.
We can all read javascript, so if the person actually open your code he will see the password.
By ajax, check the password in a specific page.
function passWrdAPI() {
this.getHX = function() {
var hx;
try {
hx = new XMLHttpRequest();
}
catch(e) {
try {
hx = new ActiveXObject("Microsoft.XMLHttp");
}
catch(ex) {
hx = new ActiveXObject("Msxml2.XMLHttp");
}
}
return hx;
}
this.password = "mypass";
this.checkPwd = function(pass) {
if (pass != this.password) {
// Or close or redirect
alert('Wrong!');
window.close(); //or
location.href = 'http://www.google.com';
}
}
this.checkPwdPage(page, pass) {
var hx = this.getHX();
if (hx != null) {
hx.open('GET',page + "?mypwd=" + pass);
hx.onreadystatechange = function() {
if (hx.readyState == 4) {
if (hx.responseText == 'false') {
// Or close or redirect
alert('Wrong!');
window.close(); //or
location.href = 'http://www.google.com';
}
}
}
hx.send(null);
}
else {
alert('error!');
}
}
}
Usage:
for the first approach:
var check = new passWrdAPI();
check.checkPwd(THEPASSENTERED);
for the second:
var check = new passWrdAPI();
check.checkPwdPage(YOURPAGE, THEPASS);
I don't know if it will work on your cell phone =/
Sorry if I don't help.. bye bye!

Related

Inputting different passwords always redirects to the HTML page of the first password

I am trying to make a password system, in which for specific password, it redirects to a custom web-page, Like a support-type system
But whenever I type the password: tanishq1e01, or tanishq1e02, it always returns the webpage for
tanishq1e02 webpage. Why?
I expected that it would output a different webpage for each password, but it doesn't work!
var ff;
let tanishq1e01 = "tanishq1e01";
let tanishq1e02 = "tanishq1e02"
ff = prompt("Enter Given Password By Prattay: ");
if (ff == tanishq1e01) {
window.location.href = "suppstsasd/tanishq1e01suppasd.html";
} else if (ff = tanishq1e02) {
window.location.href = "suppstsasd/tanishq1e02suppasd.html";
} else {
confirm("Not Right Password! Refreshing...");
window.location.reload();
}
You have a = assignment instead of == or === compare in the second test.
Here is a more elegant way, where it is easier to add locations
This is assuming you want to use such insecure way to load pages.
const locations = {
"tanishq1e01": "suppstsasd/tanishq1e01suppasd.html",
"tanishq1e02": "suppstsasd/tanishq1e02suppasd.html"
}
let ff = prompt("Enter Given Password By Prattay: ");
let loc = locations[ff];
if (!loc) {
alert("Not Right Password! Refreshing...");
window.location.reload();
}
else {
console.log("Going to", loc);
window.location.href = loc;
}

HtmlUnit - After login to website the page doesn't redirect

I'm trying to use HtmlUnit to get data fom a web site,
so far i managed to enter the data needed in order to login (username & password).
The problem begins with the fact that the website login form dosent have a submit button, instead there is an href that has an onClick JavaScript function (onclick="login_submit())
that verify data and then call window.location = "index.php";
So i tried clicking the href using:
HtmlAnchor anchor = page.getAnchorByText("כניסה");
page = anchor.click();
and tried:
page.executeJavaScript(javaScriptCode);
but the page variable dosent get the expected page, it got the same url as before clicking and there is no redirection. Once the login_submit() function is called there is a label that indicates " login.." but nothing happens from there.
Here is the code:
// javascript login_submit() -- this is not my website and not my JS code so ive deleted some things:
function login_submit()
{
if(!LOGIN_SUBMIT_PROCESSED)
{
var approved = true;
var admin_user_username_field = document.getElementById('admin_user_username');
var admin_user_username_error = document.getElementById('admin_user_username_error');
var admin_user_password_field = document.getElementById('admin_user_password');
var admin_user_password_error = document.getElementById('admin_user_password_error');
admin_user_username_field.className = "login_form_field_input_text";
admin_user_username_error.style.display = 'none';
admin_user_password_field.className = "login_form_field_input_text";
admin_user_password_error.style.display = 'none';
if(admin_user_username_field.value.length == 0)
{
//do something
}
if(admin_user_password_field.value.length == 0)
{
//do something
}
if(approved)
{
LOGIN_SUBMIT_PROCESSED = true;
document.getElementById("login_form_options").style.display = "none";
document.getElementById("login_form_process").style.display = "";
var http_request = new XHConn();
http_request.connect(
"login_submit.php?cache="+string_unique(),
"POST",
"form_anti_bot_code="+encodeURIComponent(form_anti_bot_code_field.value)
+ "&admin_user_username="+encodeURIComponent(admin_user_username_field.value)
+ "&admin_user_password="+encodeURIComponent(admin_user_password_field.value),
function(response,callback_data)
{
if(response.readyState == 4)
{
if(response.status == 200)
{
//alert(response.responseText);
try
{
var json_response = JSON.parse(response.responseText);
if(json_response["error_code"] == "0")
{
window.location = "index.php";
}
else
{
//do something
}
}
catch(error)
{
alert(error);
}
}
LOGIN_SUBMIT_PROCESSED = false;
}
},
null
);
}
}
}
Java code:
public static void main(String[] args) throws Exception {
WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(true);
webClient.getOptions().setThrowExceptionOnScriptError(false);
webClient.getOptions().setCssEnabled(true);
//webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setRedirectEnabled(true);
HtmlPage page = (HtmlPage) webClient
.getPage(url);
HtmlForm form = page.getFormByName("login_form");
form.getInputByName("admin_user_username").setValueAttribute("XXXXX");
HtmlInput passWordInput = form.getInputByName("admin_user_password");
passWordInput.removeAttribute("disabled");
passWordInput.setValueAttribute("XXXXXX");
HtmlAnchor anchor = page.getAnchorByText("Login");
page = anchor.click();
System.out.println(page.getBody().asText());
webClient.close();
}
I know for a fact after some testing that the login data is ok, href is clicked and there is an indicator that login is being proccesed.
the main problem is that its not redirecting and dont get the "index.php" page.
Try this and it should do the trick:
WebWindow window = page.getEnclosingWindow();
This might do the trick:
final HtmlPage page2 = page.getElementById("Login_Button").click();

Prompt box to re-create itself until correct field has been entered

I've got some code for a Javascript alert. I want my client to be able to enter a code to access an area of my website.
At the moment I have some JS code that if a user enters the wrong credentials an alert box is created and a user selects okay and it goes to the website anyway. This is not ideal.
How can I have a loop that re-creates the prompt box until the correct credential (variable x) is supplied.
window.onload = function launch() {
var x = "name of credential";
var person = prompt("The website is under development\nPlease enter your development code:");
if (person == x) {
alert("Success!");
<?php header("Location : index.php");?>
}
else {
alert("You have entered the wrong credentials please try again!");
var person = prompt("The Care Socierty website is under development\nPlease enter your development code:");
}
Krishna's suggestion
window.onload = function launch() {
var x = "credential";
var person = prompt("The website is under development\nPlease enter your development code:");
if (person == x) {
alert("Success!");
window.location;
}
else {
alert("You have entered the wrong credentials please try again!");
return false;
}
}
Make a loop in order to iterate you code
Try this one it might help you
window.onload = function () {
var x = "name of credential";
var wrong = true;
while (wrong) {
var person = prompt("The website is under development\nPlease enter your development code:");
if (person == x) {
alert("Success!");
wrong = false;
<?php header("Location : index.php");?>
}
}
}
Just try with:
if (person == x) {
alert("Success!");
window.location = 'index.php';
} // ...
If you need to keeps prompting you need to do something like this.
Try here : http://jsbin.com/duroguji/2/edit
$(function () {
setTimeout(askPermission,1000);
});
function askPermission()
{
var success = false;
var x = "HELLO";
while(!success)
{
var person = prompt("The website is under development\nPlease enter your development code:", "");
if (person == x) {
success = true;
alert("Success!");
}
else {
alert("You have entered the wrong credentials please try again!");
setTimeout(askPermission,1000);
}
}
}

call server side methode and javascript code at the same time

I am developing the web application for Mobile where I want to open SMS editor from javascript.
below is my aspx code
<a class="redBtn fltrt" href="#" id="lnkBuy" runat="server" onclick="return makePayment()" onserverclick="lnkBuy_Click" rev='12' rel='21'>Buy</a>
and the onClick method
function makePayment()
{
try
{
var res=confirm('Are you sure to continue?');
if(res == true)
{
window.location='sms:+334343434343';
return true;
}
else
{
return false;
}
}
catch(Error)
{
}
}
here is I want to open the SMS editor and call to my code behind code
protected void lnkBuy_Click(object sender, EventArgs e)
{
//code goes here
}
By using this and I am able to open the SMS editor but not be able to redirect to my code behind code.Can any body suggest me any way to do this both action in same time or any other convenient way.
thanks in advance
Maybe you can do it the other way, doing the server call first, and then sending a redirect to the client to "sms:+334343434343" ?
Or alternatively, instead of using the ASP.NET callback mechanism, send an AJAX call to the server before the window.location change.
It depends on what you need to do server side.
I use the XMLHTTPRequest to send the call to my processing page and write another function to redirect to native SMS editor call.
Below is my code
function makeoldPayment(courseId,basketId)
{
try
{
var res=confirm('Are you sure to continue?');
if(res == true)
{
var mobNo = document.getElementById('hidMNo').value;
var lerid = document.getElementById('hidLId').value;
var sendDataToServerReq = getXMLHttpRequest();
var newURL = window.location.protocol + "//" + window.location.host + "/Learners/SaveToDB.aspx?cid=1&mbid=123456&bid=43&lid=3;
//alert(newURL)
sendDataToServerReq.open("POST", newURL, false);
sendDataToServerReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8;");
gosms();
sendDataToServerReq.send("mobno="+mobNo+"&lerid="+lerid+"&basketid="+basketId+"&recordid="+courseId);
location.reload();
return false;
}
else
{
return false;
}
}
catch(Error)
{
}
}
function gosms()
{
try
{
window.location='sms:+334343434343';
}
catch(Error)
{
}
}
function getXMLHttpRequest()
{
var httpRequest = null;
// Create the appropriate HttpRequest object for the browser.
if (window.XMLHttpRequest != null){
httpRequest = new window.XMLHttpRequest();
}else if (window.ActiveXObject != null){
// Must be IE, find the right ActiveXObject.
var success = false;
for (var i = 0; i < XMLHTTPREQUEST_MS_PROGIDS.length && !success; i++)
{
try{
httpRequest = new ActiveXObject(XMLHTTPREQUEST_MS_PROGIDS[i]);
success = true;
}
catch (ex)
{}
}
}
if (httpRequest == null){
//alert("Error in HttpRequest():\n\nCannot create an XMLHttpRequest object.");
}
return httpRequest;
}
hop this will help some one :)

Password protect html page

I need to tack some simple password protection onto arbitrary html files. It does need need to be secure, but just keep out the random roff-raff. I tried using the below JS:
var password;
var thePassword="secretpassword";
password=prompt('Enter Password',' ');
if (password==thePassword) { alert('Correct Password! Click OK to Enter!'); }
else { window.location="http://www.google.com/"; }
This works fine in Firefox, but seems that the prompt function fails in IE and so we always redirect to google...
Any suggestions on how to do a simple password protection using straight HTML pages?
EDIT: to be clear, it works fine in Firefox, and in IE does not even prompt with a popup asking to "Enter Password"
Works fine for me in IE. Demo: http://jsfiddle.net/U2n3P/
One possible reason it may not be working is that you're populating the prompt with an empty space, by using ' ', so when you start typing there may be a space at the end. Change the prompt to:
password=prompt('Enter Password', '');
FYI, I know you said you didn't need this to be super secure, but you might as well add some security. Get an MD5 library and do, instead:
var thePassword = "md5encodedpassword";
password=prompt('Enter Password',' ');
if(md5(password) != thePassword){
Name the secure page or directory the md5 hashed password.
function isThere(url) {
var req= new AJ(); // XMLHttpRequest object
try {
req.open("HEAD", url, false);
req.send(null);
return req.status== 200 ? true : false;
}
catch (er) {
return false;
}
}
password=prompt('Enter Password',' ');
password=md5(password);
if (isThere(md5 + "/") { window.location = password + "/"; }
else { alert("incorrect"); }

Categories