How to Raise Events from VB.Net ClassLibrary/UserControl (ActiveX) to JavaScript? - javascript

I've created a VB.Net ClassLibrary with a UserControl in it. I can load it from an HTML page and call the methods that I created. This works as expected. I've tried several examples for how to raise an event from the VB code to the js caller, and none of them seem to work (I'm using IE7).
What I'm doing:
Public Class ctrlABC
Public Event TestEvent()
Then when I want to raise this event, I call:
RaiseEvent TestEvent()
In my JS file, I've tried the following:
<OBJECT id="myControl1" name="myControl1" classid="ABC.dll#ABC.ctrlABC" width=400 height=400></OBJECT>
<SCRIPT LANGUAGE=javascript FOR=myControl1 EVENT=TestEvent>
myControl1_TestEvent()
</SCRIPT>
<script>
function myControl1_TestEvent(){
alert("raised");
}
</script>
================== and then I tried ===================
<OBJECT id="myControl1" name="myControl1" classid="ABC.dll#ABC.ctrlABC" width=400 height=400></OBJECT>
<script>
function myControl1::TestEvent(){
alert("raised");
}
</script>
===========================
but neither seems to get the event. Does anyone have any ideas?
Thanks!

I've not used <object>'s like that so I cant be sure, but I would expect you declare the event handler like regular events in Javascript.
In the elements attributes:
<object id="myControl1" .... onTestEvent="functionName"></object>
or as a function in the object: (might not be the correct term)
document.getElementById("myControl1").onTestEvent = functionName
or using the event registration function(s):
document.getElementById("myControl1").addEventListener("TestEvent", functionName, false);
(these are browser specific, this one is for firefox, not sure about others)
with each of these "functionName" is a function you declare in the <head>.
Might want to have a look at Quirksmode - Advanced event registration

Related

Functions in JavaScript are not working

I created a function that could be called once the user focus on the input field; when I tried to apply and test it myself though, no function had been called. I checked for my function, it was well-written; I found no syntax mistakes. I created some tricks to find out what kind of a problem I had had: The outcome is that the same code could be applied with the event "onfocus", whereas the browser totally ignored my function even when I created the inline code within the tag "input"...Taking into notice I am using Firefox, and the test was on Firefox 52.0.2 (32-bit) on Ubuntu Linux..
The testing function code:
(In script element):
function helpMe(){
alert("Help Me!");
};
(Within the input tag):
onfous="help me()" // NOT WORKING!
(Within the input tag):
onfocus="alert('Help Me!')" // WORKING WELL!
you have a typo
<script>
function helpMe(){ alert("Help Me!"); }
</script>
(Within the input tag):
// here helpMe instead of help me ..
<input onfous="helpMe()"/>

Javascript and target="_top"

I'm working with a legacy frames website that was just moved into an iFrame.
Assuming I have the following function:
<script language = "javascript">
function myFunction(){
<!-- no console.log in IE 7 (my required target browser) -->
alert('sup, yo?');
}
</script>
and the following hyperlink triggering the function:
click me
before the move into an iFrame this worked ok. Once the website was moved into the iframe, clicking the link in IE (not FF or Chrome), I would get the ever-so-helpful error:
Line: 1
Object expected
Once I removed the target="_top" attribute the function would work, so I don't need help solving the problem, but my question is:
What is IE doing with the target attribute when calling a javascript function to invoke this behavior? I don't have other versions of IE installed, is this current behavior in 8+ as well?
Thanks.
It does not make sense to try to understand the behavior. You're using a technique that is not well defined and is not used by developers nowadays.
Instead of href="javascript:myFunction();, just use onclick="myFunction(); return false" or even better, set the handler from JS like the following
<a href="pageForUsersWithoutJs.html" id="my-link" >click me</a>
<script type="text/javascript">
// This is old school, but works for all browsers, you should use a library instead
document.getElementById('my-link').onclick = function() {
// Do your thing
return false; // so the link isn't followed
};
</script>

Android/ Phonegap - onClick() not working

I am using phonegap and am trying to detect the onClick, however Android seems to completely ignore it, below is what I am trying:
<select name="func" onclick="StartButtons();" data-native-menu="true">
I have tried several variations of the onClick include javascript, jQuery etc. however none of these seem to work.
I have noticed that onChange works, however this is not of much use to me as I need to use a prompt asking the user to confirm the change, if it is no then do nothing. However, using onchange still changes the item within the dropdown.
Can someone please help with this?
I had the same problem with jQuery, Android seems to ignore the click event in javascript. Originally, my code with jQuery looks like this:
$("#element").click(function () { alert("message"); });
I had to change it to:
$("#element").on("touchend", function () { alert("message"); });
Not sure if you're using jQuery at all, but hope this helps.
Add click event listener to your object with javascript. First add an id to object:
<select id="obj_id" name="func" data-native-menu="true">
And then call addEventListener() :
document.getElementById("obj_id").addEventListener("click", StartButtons, false);
Phonegap documentation recomends to run all your javascript code in "deviceready" event. So your code should look like this:
main.js:
function StartButtons() { ... }
function onDeviceReady() {
...
document.getElementById("obj_id")
.addEventListener("click", StartButtons, false);
...
}
function init() {
document.addEventListener("deviceready", onDeviceReady, false);
}
index.html:
...
<script type="text/javascript" charset="utf-8" src="main.js"></script>
</head>
<body onload="init();">
...
<select id="obj_id" name="func" data-native-menu="true">
...
</body>
https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
delete this code from index.html
user1183085´s answer is the right one. Android does not recognize click events as web apps do, this makes sence since mobile apps are not web when native, so that is why phonegap was made but since it works with jquery and not native java android libraries directly. I put my code this way:
The web buttons or elements onclick to handle all web interfaces, and special buttons or elements ontouchend to handle the mobile interfaces. Here´s a part of my code:
//this handles the login button events for the mobile touch of the user
$("button.login-button").on("touchend", function () {alert('hello touch ! =o' )});
//this handles the login button events for web clicks
$("button.login-button").on("click", function () {alert('hello click ! =o' )});
thanks user user1183085, that solved my life forever =()
Watch out, bind an onclick on an input in Cordova 3.4, doesn't work. But it works great with a div element. Considering, input elements are part of form element, like select element, it may be your problem.
Have you tried setting your method to be called to "method" instead of "method();"? So maybe you could try your line as:
<select name="func" onclick="StartButtons" data-native-menu="true">
Then hopefully your method will be called.

User Control in Folder Home Page Doesn't Initialize

I am programming Outlook 2003 add-in using Visual Studio 2008.
Add-in uses embedded user control in folder's home page, like as it was
recommended. Here is HTML code for folder's home page:
<html><head><style type="text/css">body{overflow: hidden}</style></head>
<body rightmargin = '0' leftmargin ='0' topmargin ='0' bottommargin = '0' onload='OnBodyLoad()'>
<script>
function OnBodyLoad()
{
var outlook = window.external.OutlookApplication;
FolderView.Initialize(outlook);
}
</script>
<object classid='clsid:C718A848-6C31-4897-8DA8-0EDE3A4C6F14'
id='FolderView' VIEWASTEXT width='100%' height='100%' />
</body>
</html>
HTML code is inserted in HTMLDocument property of the active explorer during
FolderSwitch event.
In control's OnLoad event, a reference to application instance is used
(which was passed as a parameter to its Initialize method), but sometimes
control is not initialized before OnLoad event is fired. It is just created,
but Initialize method is never invoked.
Does somebody has similar experiences? Is this usual behavior?
I have no experience with Outlook 2003 or any other version of it, BUT I know about html and JavaScript, so I would recommend to not fire the method instantly cause in some "browsers/clients" the values used inside or the things need it to continue inside the method are not available yet. You better add a delay when calling the method and maybe that will fix your problem, cause that have solved many of my problems in the past.
Example:
document.addEventListener('onload', function (e) { yourFunction(params); }, false);
NOTE: it might be onload or onbodyload.

flash fscommands and javascript

I try get the mp3 flash player to work with my javascript on all browsers. All went well for first, but fast realized that my code doesn't work on MSIE.
After trying to find out I found this in the reference code:
<!--[if IE]>
<script type="text/javascript" event="FSCommand(command,args)" for="myFlash">
eval(args);
</script>
<![endif]-->
How to turn this into a javascript or jquery clause that I could stuff it where it belongs to (in audio.js)?
That syntax, with the <script> tag with the "event" and "for" attributes is an Internet Explorer-only way of setting up an event handler on an DOM object. Here, it adds a FSCommand event handler to the myFlash object. This is needed because code running inside the Flash object may want to run JavaScript in the browser. To do this, the Flash object will invoke the FSCommand event handler, passing the JavaScript to run as the arguments to the event.
With this player, the name of a JS listener object is passed in the FlashVars param to the player. It then uses FSCommands from ActionScript to modify that listener object, with an occasional call to a method on that listener when it's other properties have been modified. I suppose that IE isn't able to run the JS code using this method until the FSCommand handler has been added to the Flash player object, which is why that code exists. Modify it to use the ID of your Flash object and you should be in good shape.
Maybe this is more about embedding flash dynamically.
I got stuck on exactly the same thing with mp3 flash player. The thing is that IE doesn't care about the special script tag with 'event' and 'for' attribute, if it is added AFTER the page has loaded. My IE wouldn't event eat jquery's .html() when the page loaded, only document.write worked. But document.write can't really be used after the page has loaded, unless it is targeted in an iframe or some other devil worship mechanism.
What's good tho, is that IE doesn't distinguish between assigning an event handler in this bastard script tag or programatically. This means that:
<script type="text/javascript" event="FSCommand(command,args)" for="myFlash">
eval(args);
</script>
in IE directly translates to:
function foo(command, args){
eval(args);
}
var ie_sucks = document.getElementById('myFlash');
ie_sucks.attachEvent("FSCommand", foo);
And... in the end we have that:
var ie_sucks = document.getElementById('comebacker_audio');
ie_sucks.attachEvent("FSCommand", function(command, args) {eval(args);});
and if that not work for you, try to check your html for inserting object. Example here:
http://kb2.adobe.com/cps/415/tn_4150.html
;)
Thanks everyone for the above. I'll just drop a few lines that were handy for me.
To re-use code across browsers, do:
<script type="text/javascript">
function mySwf_DoFSCommand(command, args) {
// do stuff
}
</script>
<!--[if IE]>
<script type="text/javascript" event="FSCommand(command,args)" for="mySwf">
mySwf_DoFSCommand(command, args);
</script>
<![endif]-->

Categories