Forum Moderators: open

Message Too Old, No Replies

javascript not working in IE but all other browser working fine

         

xuanyinwen

9:27 pm on Dec 15, 2009 (gmt 0)

10+ Year Member



Hello any one can help a beginner please.
the code is for add a password on the webpage, which working for all other browser but not for IE.

<HEAD>
<SCRIPT language="JavaScript">
<!--hide

var password;

var pass1="cool";

password=prompt('Please enter your password to view this page!',' ');

if (password==pass1)
alert('Password Correct! Click OK to enter!');
else
{
window.location="http://www.example.com/jscript/jpass.htm";
}

//-->
</SCRIPT>
</HEAD>

[edited by: Fotiman at 12:48 am (utc) on Dec. 16, 2009]
[edit reason] Examplified URL [/edit]

chasehx

11:52 pm on Dec 15, 2009 (gmt 0)

10+ Year Member



Haha you forgot to close the if statement.

add a '}' on that line above the else and you are good to go.

edit: you didn't open it either. just noticed. The if syntax is like the else syntax

if (condition)
{
...
}

Fotiman

12:47 am on Dec 16, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
1. Don't use the language attribute. It's not valid and not needed. Instead, use the type attribute:
<script type="text/javascript">

2. Don't include HTML comments in your script tag. I'm referring to the lines just below your opening script tag and just above your closing script tag.

3. Microsoft decided that "prompt" introduced a security risk of some kind, and disabled in beginning in IE7. You may get a notice in your browser, but most people won't even notice it. So your alternative is to create your own prompt dialog of sorts. I would recommend checking out the Yahoo UI Library, particularly the SimpleDialog.

4. No URLs please.

5. @chasehx, It is valid to do this:
if (condition) expression;
However, best practice is to always include the curly braces. If you have only one expression to execute, no curly braces needed (but they are still recommended).

xuanyinwen

9:35 pm on Dec 16, 2009 (gmt 0)

10+ Year Member



Hi chasehx and Fotiman:

Thanks a lot for your advice!
follow your advice, I edit the code as follow:

<script type="text/javascript">

var password;

var pass1="cool";

password=prompt('Please enter your password to view this page!',' ');

if (password==pass1)
{ alert('Password Correct! Click OK to enter!');}
else
{
window.location="test6.html";
}

</SCRIPT>

but still no luck to make it work in the IE. for the YUI, I don't have any experience about it, and now trying to find out how to use it.

would you be able to give me some more instruction please?

Many thanks!

Fotiman

2:10 pm on Dec 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The YUI SimpleDialog [developer.yahoo.com] is very well documented [developer.yahoo.com], and includes examples [developer.yahoo.com]. Start by include the YUI files you need.

<!-- Sam Skin CSS -->
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.8.0r4/build/container/assets/skins/sam/container.css">
<!-- Dependencies -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js"></script>
<!-- Source file -->
<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/container/container-min.js"></script>

Next, apply the yui-skin-sam class name to an element that is a parent of the element in which the SimpleDialog Control lives. You can usually accomplish this simply by putting the class on the <body> tag:

<body class="yui-skin-sam">

Then follow the instructions for creating a SimpleDialog. You'll need to add an input field for capturing the user input. Why don't you give that a try, and if you run into problems or have more questions, post your progress back here.

xuanyinwen

1:39 am on Dec 21, 2009 (gmt 0)

10+ Year Member



I still can't find out how it work, but now I use .htaccess to protect the webpage which is more secure as someone sugguest.

May I ask how can I find out the proper path for .htpasswd?

I find out one from website hosting company, but not the other one.

thanks

rocknbil

6:38 am on Dec 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It can be anywhere you want, and is determined by the directives in the .htaccess.

The .htaccess of the protected directory contains

AuthUserFile /some/path/to/pwd-file/preferably/off/root/.htpasswd
AuthName "This is a protected directory"
AuthType Basic
<Limit GET>
require valid-user
</Limit>

To create the .htpassword for that directory, from the command line enter

htpasswd -c /some/path/to/pfile/preferably/off/root/.htpasswd user-name

And you will be prompted for a password . . . done. The -c flag creates the .htpasswd file. Notable the mention, the file does not HAVE to be named .htpasswd, but it is a hidden file by default if named .htpasswd.

basic authentication tutorial [colostate.edu].
htpaswd documentation [httpd.apache.org]