开发者

Javascript not work in IE browser, but work fine for firefox or Chrome [duplicate]

开发者 https://www.devze.com 2023-02-20 19:45 出处:网络
This question already has an answer here: Closed 11 years ago. Possible Duplicate: JavaScript works under Firefox but does not work under IE
This question already has an answer here: Closed 11 years ago.

Possible Duplicate:

JavaScript works under Firefox but does not work under IE

The script below is not working for IE. Don't worry about whether it's secure or not, I just want to test this javascript but I have no idea why it's not working on IE. When I open it on IE, it will treat it as wrong password and go to the google page stra开发者_JAVA技巧ight away. The alert for entering the password is not executed. However, it's working fine for other browsers. I have tried many different version of IE and disabled the block add-on but still no luck to make it work.

<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.google.com/";
    }

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


Works for me in IE6 and IE8, although I do receive a security warning in both that I have to click through.

If you add the line

window.alert('received password: ' + password);

directly under the prompt line, what do you see as the password? Does this shed any light on the problem?

Also, where are you running this from (file/web server) and what is Internet Explorer reporting as the security zone (on bar at the bottom of the browser, towards the right - probably says something line Internet or Local Intranet or something)?

[EDIT - in response to comment from OP]

You've just revealed you are running this on the server. On Windows 2008 servers (possibly 2003 - can't remember), Internet Explorer runs in something called Internet Explorer Enhanced Security Mode (it probably says this on its "home page" when you start it up). In this mode, security is ultra tight and it won't run Javascript and lots of other stuff from anything but trusted sites, and it won't even warn you. This is to limit the chance you can accidentally introduce a virus, trojan or whatever to your server just by web browsing form it. The behaviour you describe now makes total sense.

Unfortunately, I don't think you can add local files to the list of trusted sites (Tools->Internet Options -> Security) so there is no simple way of allowing this file.

You choices are:

  • Disable Internet Explorer Enhanced Security. Possibly not recommended, unless you do it for a non-privileged user only.
  • Serve this file via the web server on your server, not directly from the file - then you can debug it up over http which you can add to the trusted sites list.
  • Don't try and debug this stuff on the server at all (at least, not in IE on the server).


Take a look at this page:

http://www.hunlock.com/blogs/Working_around_IE7s_prompt_bug,_er_feature

EDIT: If that doesn't help, take a look at jQuery. There is a plugin specifically to replace that prompt and avoid IE7 issues (so they say, at least):

http://abeautifulsite.net/blog/2008/12/jquery-alert-dialogs/

jQuery will solve many of the cross-browser compatibility issues of which your post is an example. I resisted it myself for some time, but now I'm glad I started using it.

EDIT 2: This will work in IE7 (no prompt, though)

<html>
    <head>
        <script type="text/javascript">
        function checkPasswd() {
        var passwd = document.getElementById ("passwd");
        if (passwd.value === "cool1")
            alert('Password Correct! Click OK to enter!');
        else
            window.location="http://www.google.com/";
    }
    </script>
    <body>
        <form>
            <input name="passwd" id="passwd" type="password"/>
            <input name="submit" type="button" value="submit" onclick="checkPasswd();"/>
        </form>
    </body>
</html>


Google the IE7 prompt bug which is probably what you're experiencing.

0

精彩评论

暂无评论...
验证码 换一张
取 消