SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Alternative Event Handler to <script event=[...] for=[...]></script>?

    Hi All!

    I'm looking for some method of handling events on an <object> element other than using
    Code:
    <script event="oncustomevent(params)" for="objectname"></script>
    Since I will be manipulating the OBJECT in question through a JavaScript class, I was hoping there may be a way to cleanly handle events from within the class.

    Thanks!
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got this working a while back in IE - Firefox won't work (I don't think it supports the htmlFor method? and/or maybe your <object>). Sample is written for a simple object.

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var something = function () {
    	alert("hello");
    	// do some stuff
    	var t = document.body.appendChild(document.createElement("span"));
    	t.innerHTML = "testing";
    	t.style.backgroundColor = "red";
    }
    
    window.onload = function () {
    	var btn = document.getElementById("mybutton");
    	var s = document.createElement("script");
    	s.htmlFor = "mybutton";
    	s.event = "onclick";
    	s.text = "(" + something.toString() + ")()";
    	document.body.appendChild(s);
    }
    </script>
    </head>
    <body>
    <button id="mybutton">click me</button>
    </body>
    </html>

  3. #3
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the response. Unfortunately my situation is that of not being able to use standard events. Rather, the object in question is an ActiveX control (or Firefox add-in, depending on the browser). The event parameter in
    Code:
    <script event="oncustomevent(params)" for="objectname"></script>
    is actually an event that is triggered by the ActiveX/AddIn. This means that it isn't using standard user events.

    I have not been able to get these to work in conjunction with standard event handling methods so far.

    Any ideas? Thanks
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My posted code should work (in IE anyway) - just change this line:
    s.event = "onclick";
    to
    s.event = "oncustomevent(params)";

    I have used it before to control WMP, and the event was:
    scrScr.event = "ScriptCommand(bType, bData)";

    So it's not relying on standard user events.

  5. #5
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot for your help! It seems to work in IE. Now I need to figure out how I can get it to work in FF as well.
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Maybe this will help:
    Code:
    var event = document.createEvent("nsDOMMyEvent");
    event.initEvent("nsDOMMyEvent", true, true);
    window.dispatchEvent(event);
    from http://developer.mozilla.org/en/docs..._Can_Pass_Data


    Also might be worth a try looking/asking in the mozillazine forums.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •