简体   繁体   English

JavaScript无法在IE上运行,但在所有其他浏览器中都可以

[英]JavaScript not working on IE, but OK in all other browsers

The code is for adding a password on the webpage, which working for all other browser but not for IE. 该代码用于在网页上添加密码,该密码适用于所有其他浏览器,但不适用于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.pageresource.com/jscript/jpass.htm";
}

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

window.location doesn't work in IE6. window.location在IE6中不起作用。 You probably want document.location . 您可能需要document.location

if ("cool" == prompt('Please enter your password to view this page!', '')) {
    alert('Password Correct! Click OK to enter!');
} else {
    document.location = "http://www.pageresource.com/jscript/jpass.htm";
}

IE7 has the prompt functionality disabled by default. IE7默认情况下禁用了提示功能。

And as everyone before me has said unless this is just a learning javascript type endevour - the "protection" is useless. 就像我之前的每个人都说过的那样,除非这只是一种学习性的JavaScript类型,否则“保护”是没有用的。 All anyone has to do is disable JS and/or look at your source for where youre redirecting. 任何人所要做的就是禁用JS和/或查看您的重定向源。

Try wrapping curly braces around your if: 尝试在以下情况下将花括号括起来:

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

You probably don't really need that hide stuff anymore either. 您可能也不再真的需要隐藏的东西。 Good luck on your learning. 祝您学习顺利。

Ok, firstly I hope you are just doing this to play with javascript because anyone will be able to bypass your security in a snap just by looking at the source code. 好的,首先,我希望您只是这样做以与javascript一起玩,因为任何人都可以通过查看源代码迅速避开您的安全性。 To manage login access to a site you really should be using a server side implementation such as php, jsp, asp etc. Those sites take the password login and compare it on the server which the normal user cannot see. 要管理对站点的登录访问,您实际上应该使用服务器端实现,例如php,jsp,asp等。这些站点采用密码登录并将其在普通用户看不到的服务器上进行比较。 Anyway your code below should work: 无论如何,您下面的代码应该可以工作:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
    <head>
        <title>HTML Test</title>
        <script type="text/javascript">
            function check()
            {
                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
                    document.location="http://www.pageresource.com/jscript/jpass.htm";
            }
        </script>
    </head>
    <body onload="check()">
    </body>
</html>
  1. I think you are missing one squiggly at end of 'if' line, and another at beginning of 'else' line, or, remove the two that are existing now . 我认为您在'if'行的末尾迷失了一个,而在'else'行的末尾迷失了一个,或者,删除现在存在的两个。

So, either this: 因此,要么:

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

.. or this (although I never code this way personally) .. ..或这个(尽管我从来没有亲自写过)

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

should work. 应该管用。

Also, try window.location.href instead of just window.location . 另外,尝试使用window.location.href而不是window.location

window.location.href = "http://www.pageresource.com/jscript/jpass.htm";

Of course, as mentioned ny others, your approach is not secure using password on client side ( <!--hide does NOT hide the javascript from the source code view ). 当然,正如其他人提到的那样,在客户端使用密码是不安全的<!-hide不会在源代码视图中隐藏javascript )。

Hope this helps - 希望这可以帮助 -

it looks like your first line of js: 它看起来像您的第一行js:

<!--hide

should be : 应该 :

// <!-- hide

But it looks liek you may have other problems as well. 但是看起来您可能还有其他问题。 Try that first! 首先尝试!

If my JavaScript only breaks in IE, the very first thing I do is run it through JSLint , a free tool that checks your JS code for errors. 如果我的JavaScript只在IE中中断,我要做的第一件事就是通过JSLint运行它,这是一个免费工具,可检查您的JS代码是否有错误。 This will likely reveal what's causing IE to choke. 这可能会揭示导致IE阻塞的原因。

DON'T DO THAT!!! 不要这样做!!! You're sending the user the password. 您正在向用户发送密码。 All the user has to do is "View Source" and it's right there. 用户要做的就是“查看源代码”,就在这里。 Alternatively, the user can get whatever information the password was hiding from the Javascript source. 或者,用户可以从Javascript源中获取密码隐藏的任何信息。

Client-side validation is great for things like data formats, but nothing that is sensitive should ever be done client-side, and all data should be validated again on the server. 客户端验证非常适合诸如数据格式之类的事情,但是客户端不应该执行任何敏感的操作,并且应该在服务器上再次验证所有数据。 Remember, a malicious user can send you anything, bypassing your Javascript entirely. 请记住,恶意用户可以向您发送任何内容,从而完全绕开您的Javascript。

its this bit of code here 这是这段代码

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

first of all wrap the alert bit in curly braces to read 首先将alert位用大括号括起来以读取

if (password==pass1){
  alert('Password Correct! Click OK to enter!');
}

secondly, unlike php and other C like languages, do not start braces in a new line... 其次,与php和其他类似C的语言不同,不要在新行中开始大括号...
in javascript, when a script is evaluated by the interpreter, it looks for a semicolon designating the end of a statement, 在javascript中,当解释器评估脚本时,它会寻找一个分号来指定语句的结尾,
if a semicolon is not found, the closest previous line break is replaced with a semicolon 如果找不到分号,则将最近的前一个换行符替换为分号

So, your else block is evaluated as : 因此,您的else块评估为:

else;
{;
window.location="http://www.pageresource.com/jscript/jpass.htm";
};

instead write it as 而是写成

else{
  window.location="http://www.pageresource.com/jscript/jpass.htm";
}

When I used the IETester 's "open URL in all IE versions", it failed to work in all windows expect of the first window where the prompt was entered, regardless of the IE window used. 当我使用IETester的“所有IE版本中的开放URL”时,无论使用哪个IE窗口,它都无法在输入提示的第一个窗口的所有窗口中工作。

However, it works fine here in all known browsers (including IE6/7/8) when tested individually . 但是,如果单独测试,它在所有已知的浏览器(包括IE6 / 7/8)中都可以正常工作。 I haven't change anything from the code as outlined here . 我没有对这里概述的代码进行任何更改。

So your actual problem lies somewhere else than in the as far provided information. 因此,您实际的问题不在提供的其他信息中。

Needless to say, this approach is far from secure. 不用说,这种方法远非安全的。

FROM window.location TO document.location is all you need to change for a bug-free cross-browser implementation of your code. window.locationdocument.location是您为代码的无错误跨浏览器实现window.location全部更改。 Never use window unless you are truly referencing the window object; 除非您真正引用了窗口对象,否则切勿使用窗口; Example: window.close() 示例: window.close()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript无法在IE中运行,但所有其他浏览器都可以正常运行 - Javascript not working in IE, but all other browsers are working fine javascript按钮在IE中不起作用; 在其他浏览器中工作 - Javascript button not working in IE; working in other browsers Chrome中的Javascript链接错误在所有其他浏览器中都可以正常运行? - Javascript Link error in Chrome which works ok in all other browsers? jQuery代码无法在IE中运行,但可以在所有其他浏览器中运行 - Jquery code not working in IE but working in all other browsers Window.print()不能在IE中工作,但可以在所有其他浏览器中工作 - Window.print() is not working in ie, but working in all other browsers JavaScript代码可在其他浏览器上运行,但不能在IE11上运行,为什么? - JavaScript code working on other browsers but not working on IE11, why? 仅在IE中出现JavaScript错误,在所有其他浏览器中均有效 - JavaScript error in IE only, works in all other browsers JavaScript的:形式是没有得到submiited只有IE浏览器,但是,它的工作原理所有其他浏览器 - javascript: Form is not getting submiited only IE, but, it works all other browsers javascript图片幻灯片可在所有其他浏览器上运行,但不能在IE上运行 - javascript image slideshow works in all other browsers but not IE Flash菜单的Javascript未在IE9中加载,但在所有其他浏览器中都可以正常运行 - Javascript for flash menu not loading in IE9 but fine in all other browsers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM