Javascript help please

Draygoes

Fully Optimized
Messages
1,663
Location
United States
Ok, so I really need to brush up on my Javascript skills, which I have been actively doing.

This is what I need help with. I found a basic password prompt script that I have been using to keep script kiddies off of my page. I am not protecting anything too important, but I dont exactly want traffic. That having been said, I decided to go with this;
Code:
<SCRIPT language="JavaScript">
<!--hide

var password;

var pass1="cool";

password=prompt('Password!',' ');

if (password==password)
  {
Return;
}
else
{window.alert("Wrong!");
}
   {
    window.location="http://google.com";
    }

//-->
</SCRIPT>

The largest modification that I made, was to add "Return;" in place of the alert box that was there. I just want a quick and simple way to exit the code. In most cases, this works fine. However, in IE I get an error message. I will attach a screen cap at the end of this post.
That normally wouldnt be an issue, but I designed a portable app for windows that loads an IE object, then the site so that I dont leave the site in the history of computers that are not mine. (And also because I wanted to.) Another reason that I wish to fix this, is because I wish to learn. Eventually I will be slapping javascript into websites designed for the public, so I need to get back into the habbit.

That was a very long way of saying, that I wish to ask a simple question. I googled for ways to exit a javascript function, and the answer was to use "Return;". Does anyone here know a better way that will not upset IE?

Oh, and it should be noted that it only causes the error message when being ran from my program with the imbedded IE object. Not sure why that makes a difference, but the normal browser doesnt do that.

Thank you all for your time.

And of course, the screen shot...

osh6ix.png
 
I don't see how this code will work since the line

if (password==password)

will always resolve to true. Is that line a typo? Shouldn't it read

if (password==pass1)?

Sorry, can't help with the Return issue.
 
I am not sure, as I borrowed the code. All I know is, it works perfectly as is besides for the return issue. I can change the password==password to any password that I want. Example "password==yogi". Its just reading from the prompt and compairing to a preset. At least, that is the way that I have understood it. I am using "password==Jerry" right now. You know, keeping it simple.
I actually have no idea why they set it up as the following, as the pass1 variable is does not appear to be called on anywhere else in the script, but I know that it doesnt work at all if I dont leave it in.
Code:
var password;  
var pass1="cool";
I am just looking to replace return with somthing more along the lines of
"Application.ExitScript();". That is lua, and it does exactly what it says it does. I dont have to return an undefined value in lua. Oh, and if I define return, the script crashes completely.

And yeah, I see what you are getting at because in pretty much every other language, that wouldnt work. But in this case, it does. Meh, I have been out of the JS game too long.

Thank you for the quick reply btw.

EDIT
I also wish to clarify a few things.
First; When I say that I borrowed the script, I mean that I got it from a JS coding website that I am using to relearn what I once knew. I just modified the script to fit my needs. I suppose by defination that makes me a script kiddie, but that hardly fits the way I normally do things. Borrowing code is just somthing that I do on my way to learn. I do not make a habit of it.

Second; The unmodified version was using an alert prompt, but I find two prompts to get one thing done to be excessive and annoying. That is why I am looking for a way to simply stop the execution in its tracks. The alert box did that just fine, but again, it was annoying.

Third; Before anyone points out the obvious, of course I know that all someone would have to do to get the password is to view the source code. For the most part, the site is off the grid anyway. I just want to keep prying eyes away should some random person stumble upon it. I am not trying to keep anyone whom knows anything about coding out, as I already know that wouldnt work. I can use a JS encoder or somthing, but that would only slow someone down.
 
Last edited:
Here is the exact script as I have it set right now...
Code:
<SCRIPT language="JavaScript"> 
<!--hide  
var password;  
var pass1="cool";  
password=prompt('Password!',' '); 
 if (password==Jerry)   
{ 
Return; 
} 
else 
{window.alert("Wrong!"); 
}    
{     
window.location="http://google.com";     
} 
 //--> 
</SCRIPT>

It prompts for a password, if correct exits. If wrong, redirects you to google. I can also have it loop back to the same page asking for the password again if I wish.

This works just fine everywhere with the acceptation of my app. Its driving me nuts because no matter what I change I get different errors, most of which crash the script before its allowed to even begin executing.
 
Last edited:
The way that script is written is correct, it's different than what you posted earlier.

However, instead of using

if (password==jerry), it is meant to be (technically it should be =="jerry")

if(password==pass1)

and if you want the password to be jerry, you should change the line

var pass1="cool";

to

var pass1="jerry";

otherwise, the pass1 variable is declared but never used and that line can be dropped entirely.
 
Code:
Return;
Should be:
Code:
return;

Coding is case sensitive. You're getting that error because it's now expecting a method that doesn't exist.

Also, you should NEVER hard code passwords. Some programmers might say this is okay, and for some languages it very well may be, but .NET is completely decompile-able. Meaning, anyone with a little bit of know-how will just look in your code and grab the password.

You'll want to use a web.config, and specifically an AppSettings container to store the password and pull from there. Then use encryption to secure that file.

See here.
 
Last edited:
Thanks guys!
@root: As I stated above in the thread, I am not to worried about most people getting the password. The page is hardly worth protecting. I am just using this as a launching platform to learn JS again, as its been years.
That being said, thank you for the link. I have bookmarked it for now. :)
 
I suggest using Codecademy, if you're just brushing up on Javascript. It's an interactive training platform, and it's FREE (no catches). Among other languages, there is both a course on Javascript, and jQuery. It'll be helpful to cover the basics, and may help you out. :)
 
I suggest using Codecademy, if you're just brushing up on Javascript. It's an interactive training platform, and it's FREE (no catches). Among other languages, there is both a course on Javascript, and jQuery. It'll be helpful to cover the basics, and may help you out. :)
Thanks! I have no idea how this is the first time I have seen that site. Seems like it would be suggested more often. :rolleyes:
That is yet another site to bookmark. I think I am going to start storing my bookmarked sites in a text file on GD or somthing. :)
 
Thanks! I have no idea how this is the first time I have seen that site. Seems like it would be suggested more often. :rolleyes:
That is yet another site to bookmark. I think I am going to start storing my bookmarked sites in a text file on GD or somthing. :)

Yeah, it's a winner for sure! :) The community are very helpful, and it's FREE. :D If you need any help or suggestions as you go along, feel free to send me a message!
 
Back
Top Bottom