开发者

what are the "for" and "event" attributes of the script tag (Javascript,HTML)

In a web application I've inherited at work which was written about 10 years ago I've noticed the fo开发者_开发百科llowing code snippets repeatedly used:

<script language="JavaScript" for="FG1" event="Mousedown(Button, Shift, x, y)">
{
   // some code here that uses the variables Button, Shift, x and y
}
</script>

I've never really seen anything like this before. FG1 is an active x object so are these some special things for it specifically or are they just another way of handling any regular javascript event...could the ID reference an input (e.g. a button) and the event be onclick?

ideally, i'd re write it as (if my thinking is correct...I'm not actually going to change the code in the web app as it works, i just want to understand what it means!)

<script type="text/javascript">
    var fg1 = document.getElementById("FG1");
    fg1.onMouseDown = function(Button, Shift, x, y) {
        // do stuff here...
    }
</script>


Those are Microsoft-specific (Internet Explorer-only) extensions to the script tag, and your impulse to rewrite the example without them is a good one.


According to MSDN, the:

for attribute:

Sets or retrieves the object that is bound to the event script.

event attribute:

Sets or retrieves the event for which the script is written.

Therefore, I presume as you have that you can drop the non-standard attributes and use the lines you added to get the element, and handle the mousedown event.


for attribute is for the element name in for attribute like for="element1" and event attribute is for event handling like even onclick, onmouseover etc for that elements.

For example if you add Onclick event then onclick event works on element which name you entered in for attribute.


I have seen this kind of code snippet in a classic ASP project, where it uses a simple vbscript form validation method. `

<input name="button1" type="button" id="button1" value="Submit">
    <script language="VBScript" for="button1" event="onClick">
                Menu_Validate()
              </script>

This onclick event will call the Menu_Validate() method and do form validation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜