javascript events in included file?

i have a comm object that raises events and javascript on the web page to capture those events.  It all works perfectly.  Now I have to "distribute" this javascript to serveral other web applications for use on their web pages (this is all internal intranet stuff).

here is the javascript function and script tag for the event sink:
<script for="ica" event="OnAgentEvents(evd)" language="javascript">
        function ica::OnAgentEvents(evd) {
           // do stuff here
        }
</script>

Open in new window


how can I put this into a seperate .js file?  or does it have to remain on the page itself?

BTW, the "ica" object is on the page in an object tag:
<object classid="clsid:55527FAD-5E60-4F44-867B-9F6E7B0DB9DD"
                     id="ica" name="ica" >
</object>
dhenderson12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Duy PhamFreelance IT ConsultantCommented:
You can't put
<script for="ica"....></script>

Open in new window

in external .js file and then include it in pages having COM object.

But you can do it in different way:

1. Include all COM Event Handlers in one .js file


function ica_Event1Handler(params) {
...
}

function ica_Event2Handler(params) {
...
}

Open in new window

2. Create a function that embed your COM object with handler binding scripts


function injectCOMObjectWithhandlers(ContainerId, COMObjectId) {
    var embeddedHtml = '<script for="' + COMObjectId + '" event="OnEvent1(eArgs1)" language="javascript">ica_Event1Handler(eArgs1);</script>';
    embeddedHtml += '<script for="' + COMObjectId + '" event="OnEvent2(eArgs2)" language="javascript">ica_Event2Handler(eArgs2);</script>';
    embeddedHtml += '<object classid="clsid:55527FAD-5E60-4F44-867B-9F6E7B0DB9DD" id="' + COMObjectId + '" name="' + COMObjectId + '"></object>';

    $('#' + ContainerId).html(embeddedHtml);
}

Open in new window

3. Then include that .js file in your pages, and call the injectCOMObjectWithhandlers() function on page load or whenever you want


For example:
window.onload = function() {
    injectCOMObjectWithhandlers('IDOfCOMParentDivElement', 'ica');
};

Open in new window

0
dhenderson12Author Commented:
i'm trying to implement your solution, but have a couple questions:
1.  what would the containerid be in step #2?
2.  where would the code in step #2 live?  in the external file with the events?
0
dhenderson12Author Commented:
never mind my last post.  I see the answers to my questions.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Duy PhamFreelance IT ConsultantCommented:
You might already figure it out, but I just want to express my idea:
1. I leave containerid as parameter to inject method to support the case of different COM Object Id in different page, or you might want to have more than one COM Objects in one page which need different Ids.
2. Code in step #2 can be in the same as #1, it is just a function
0
Duy PhamFreelance IT ConsultantCommented:
Sorry for my wrong read for your #1 question. ContainerId is Id of the place holder for the COM Object, which might be useful when you want to allow resizing of the COMObject which might not easy to set it directly to the COMObject. Also inject function should know where to inject the COMObject into.
0
dhenderson12Author Commented:
I am having problems with your solution:
1.  you are embedding the event handler in the page div where the object lives.
2.  my event handler currently lives in the page header;  this is what I want to put into an external .js file.
3.  the event handler is quite extensive, parsing the parameter to determine what fields to change, etc.

My biggest problem is that the event method ica::OnAgentEvents(evt) returns an object not found error for ica.  If you can help me get around this I would really appreciate it.  

Thanks for your help thus far.
0
Duy PhamFreelance IT ConsultantCommented:
1 & 2. You can change the inject function to place the event handlers on head tag, for example:

$('<script />').attr({ type: 'text/javascript', for: COMObjectId, event: 'OnEvent1(eErgs1)' }).html('ica_Event1Handler(eArgs1);').appendTo('head');

Open in new window


But then remember to append event handlers to HEAD section before injecting COM Object to get events bound when COM Object is fully loaded.

3. not sure what you are asking here?

When you define your function like <COMObjectId>::Event(params), then you must explicitly add <object id="COMObjectId" ....> into your page at design time. That's why you got 'Object not found error for ica' when you dynamically add <object ...> from code. Just remove <COMObjectId>:: part from your event handler definitions and also when injecting the COM Object.

Also when you use <COMObjectId>::Event(params), then you don't need <script ... for="COMObjectId" event="Event(params)"></script>. However those <COMObjectId>::Event(params) can't just be put into external file (I still don't really know why), that's why we had to do dynamic inject COM Object and Event Handlers in our project.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dhenderson12Author Commented:
ok, I understand.  finally got it all working.  thanks for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.