从 ASP 调用 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14460246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:51:13  来源:igfitidea点击:

Calling JavaScript from ASP

javascriptasp-classic

提问by JGGR13

I have a line of code:

我有一行代码:

Response.write("<script language=""text/JavaScript"">alert(""What up dog"");</script>")

This doesn't work. I see no alert box, yet I see the page source has written the code correctly:

这不起作用。我没有看到警告框,但我看到页面源代码已正确编写:

<script language="text/JavaScript">alert("What up dog");</script>

What I'm actually trying to do is this:

我真正想做的是:

Response.write("<script language=""text/JavaScript"">document.cookie = '" & Cookie & " = ; expires=Thu, 01 Jan 1970 00:00:01 GMT;';</script>")

That is: Delete a cookie with the name stored in the ASP variable 'cookie'. This doesn't work either, which is why I'm attempting to create the alert box just to test where I'm screwing up.

即:删除名称存储在 ASP 变量 'cookie' 中的 cookie。这也不起作用,这就是为什么我试图创建警报框只是为了测试我在哪里搞砸了。

I've tried deleting the cookie with pure ASP (Response.cookie(Cookie).expires = Now() - 1), but since I made the cookie with JavaScript, it's not HTTPOnly so I can't access it with ASP. (I just learned this, so I'm not 100% on the why of it all, but there it is.)

我试过用纯 ASP 删除 cookie (Response.cookie(Cookie).expires = Now() - 1),但由于我用 JavaScript 制作了 cookie,它不是 HTTPOnly,所以我不能用 ASP 访问它。(我刚刚学会了这个,所以我不是 100% 知道这一切的原因,但就是这样。)

So, back to the first line of code, why am I not seeing a JavaScript alert box with that line of code? I'm obviously missing something simple (it's always something simple).

那么,回到第一行代码,为什么我没有看到带有该行代码的 JavaScript 警告框?我显然错过了一些简单的东西(它总是很简单)。

回答by Cobra_Fast

The language=""attribute for <script/>-tags is deprecated and erroneous values prevent scripts from being executed in many browsers.

-tags的language=""属性<script/>已被弃用,错误的值会阻止脚本在许多浏览器中执行。

To me it looks like you were heading for the type=""attribute.

对我来说,您似乎正在寻找该type=""属性。

Try the following code:

试试下面的代码:

Response.write("<script type=""text/javascript"">alert(""What up dog"");</script>")