放“javascript:”在代码面前? [英] Putting "javascript:" in front of code?

查看:60
本文介绍了放“javascript:”在代码面前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到一些这样的代码示例:


< DIV onmouseover =" this.style.background =''blue''">


和其他代码示例如下:


< DIV onmouseover =" javascript :this.style.background ='' blu e''">


哪种方式更合适?或两种方式都完美无缺?是否有任何

规范讨论何时javascript :应该放在代码前面吗?

I see some code examples like this:

<DIV onmouseover="this.style.background=''blue''">

and other code examples like this:

<DIV onmouseover="javascript:this.style.background=''blu e''">

Which way is more proper? Or are both ways perfectly fine? Are there any
specifications that discuss when "javascript:" should be put in front of code?

推荐答案



< de ****** *@no.spam.com> schreef in bericht

新闻:3f *************** @ news.md.comcast.giganews.co m ...

<de*******@no.spam.com> schreef in bericht
news:3f***************@news.md.comcast.giganews.co m...
我看到一些这样的代码示例:

< DIV onmouseover =" this.style.background =''blue''">

和其他代码这样的例子:

< DIV onmouseover =" javascript :this.style.background =''blu e''">
规范讨论何时javascript :应该放在
代码前面?
I see some code examples like this:

<DIV onmouseover="this.style.background=''blue''">

and other code examples like this:

<DIV onmouseover="javascript:this.style.background=''blu e''">

Which way is more proper? Or are both ways perfectly fine? Are there any
specifications that discuss when "javascript:" should be put in front of code?




第一个是正确的方法,因为onmouseover属性(和

其他事件处理程序)期望该值仅包含JS代码。


您经常会看到的是javascript的使用 :pseudo 协议:

< a href =" javascript :popup(''somepage'')"> ...< ; / a>


应替换为:

< a href =" somepage"的onclick = QUOT;弹出(HREF);返回false"> ...< / a>

因为在禁用JS时该链接仍然可以点击。当JS启用
时,将使用href值作为参数调用该函数,并且

最后的return false会阻止默认操作是



参见: http://jibbering.com/faq/#FAQ4_24

JW



The first one is the proper way, because the onmouseover attribute (and
other event handlers) expects the value to contain JS code only.

What you will also see very often, is the usage of the javascript: pseudo
protocol in anchors:
<a href="javascript:popup(''somepage'')">...</a>

which should be replaced with:
<a href="somepage" onclick="popup(href); return false">...</a>

because the link will still be clickable when JS is disabled. When JS is
enabled, the function will be called with the href value as an argument and
the ''return false'' at the end prevents that the default action will be
taken.

See also: http://jibbering.com/faq/#FAQ4_24
JW


Janwillem Borleffs于2003年12月6日写道:
Janwillem Borleffs wrote on 06 Dec 2003:

< de ******* @ no.spam.com> schreef在bericht
新闻:3f *************** @ news.md.comcast.giganews.co m ...

<de*******@no.spam.com> schreef in bericht
news:3f***************@news.md.comcast.giganews.co m...
我看到一些代码示例如下:

< DIV onmouseover =" this.style.background =''blue''">

和其他代码示例如下:

< DIV onmouseover =" javascript :this.style.background =''blu e''">

哪条路更合适吗?或两种方式都完美无缺?
是否有任何规范可以讨论什么时候javascript :"
应该放在代码前面?
I see some code examples like this:

<DIV onmouseover="this.style.background=''blue''">

and other code examples like this:

<DIV onmouseover="javascript:this.style.background=''blu e''">

Which way is more proper? Or are both ways perfectly fine?
Are there any specifications that discuss when "javascript:"
should be put in front of code?


第二种方式不正确。 "的JavaScript的 的:"是一个URI协议

说明符,因此它仅在src或href属性中有效。然而

(如J Borleffs所述),不应使用JavaScript URI

用户已禁用JavaScript:URI格式错误

对用户没用。

第一个是正确的方法,因为onmouseover
属性(和其他事件处理程序)期望值包含JS代码只有。


The second way is incorrect. "javascript:" is a URI protocol
specifier, so it is only valid in a src or href attribute. However
(as J Borleffs stated), JavaScript URIs should not be used in case
JavaScript has been disabled by the user: the URI will be malformed
and useless to the user.
The first one is the proper way, because the onmouseover
attribute (and other event handlers) expects the value to
contain JS code only.




什么?!?!这太糟糕了,这是不真实的!任何脚本语言都可以使用内在事件:这就是为什么它们是HTML

规范的一部分,而不仅仅是JavaScript。


内部事件中使用的语言使用HTTP

标头指定,或者更常见的是,放置在文档中的META元素

HEAD:


< META http-equiv =" Content-Script-Type" type =" script_MIME_type">


对于JavaScript,MIME类型是text / javascript,所以上面应该是

读取:


< META http-equiv =" Content-Script-Type" type =" text / javascript">


如果省略标题或META元素,但使用内在的

事件,则HTML文档无效。它之所以能够工作的唯一原因是因为浏览器会对

内在事件包含的内容做出假设(它们通常假设是JavaScript)。然而,

并非所有人都可以假设JavaScript,因为没有默认语言。实际上,HTML规范明确指出,如果没有默认语言,则无需假设需要

,任何事件都可以忽略

由作者指定(使用META或HTTP标题)。


<剪掉其余的回复,这是OK>


Mike


-

Michael Winter
M。****** @ blueyonder.co.inva lid(将.invalid替换为.uk)



WHAT?!?! That is so WRONG it''s untrue! The intrinsic events can be
used by ANY scripting language: that''s why they are part of the HTML
specification, not solely JavaScript.

The language used in intrinsic events is specified either with a HTTP
header or, more commonly, a META element placed in the document''s
HEAD:

<META http-equiv="Content-Script-Type" type="script_MIME_type">

For JavaScript, the MIME type is text/javascript, so the above should
read:

<META http-equiv="Content-Script-Type" type="text/javascript">

If you omit either the header or META element, but use intrinsic
events, your HTML document is invalid. The only reason why it would
work is because the browser makes assumptions about what the
intrinsic events contain (they usually assume JavaScript). However,
not all may assume JavaScript as there are no ''default'' languages. In
fact, the HTML specification states clearly that NO assumption needs
to be made and any events can be ignored, if no default language has
been specified by the author (using META or HTTP header).

<snipped the rest of the reply, which is OK>

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk")




Michael Winter < M ****** @ blueyonder.co.invalid>。 schreef in bericht

新闻:Xn ******************************* @ 193.38.113。 46 ...

"Michael Winter" <M.******@blueyonder.co.invalid> schreef in bericht
news:Xn*******************************@193.38.113. 46...

什么?!?!这太糟糕了,这是不真实的!内部事件可以被任何脚本语言使用:这就是为什么它们是HTML
规范的一部分,而不仅仅是JavaScript。

WHAT?!?! That is so WRONG it''s untrue! The intrinsic events can be
used by ANY scripting language: that''s why they are part of the HTML
specification, not solely JavaScript.




是的,你当然是对的。

JW



Yeah, you''re right of course.
JW


这篇关于放“javascript:”在代码面前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆