473,799 Members | 3,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Late binding / dynamic dispatch: using a function not yet defined

16 New Member
I searched, I swear. All my keywords were ignored by the search engine. Or I may just suck at searching with the right keywords, but I've been unable to find information about this. Perhaps somebody can elaborate.

I would to like to create a function, included in the header of an HTML page, which calls functions which are defined later, in <script /> tags farther down in the body of the page (I understand this is a terrible idea, but it is a constraint that I have to work with). Unfortunately, the intuitive way (but inherently impossible) of doing this doesn't work:
Expand|Select|Wrap|Line Numbers
  1. function myEarlyFunction() {
  2.   if(typeof myLateFunction != 'undefined') {
  3.     myLateFunction();
  4.   }
  5. }
  6. ...
  7. ...
  8. ...
  9. <script type="text/javascript">
  10. function myLateFunction() {
  11.   //do stuff
  12. }
  13. </script>
Is there a way to do this that...well, works? And if so, how?

Thanks so much!
Aug 15 '07 #1
4 2713
misaw
17 New Member
I searched, I swear. All my keywords were ignored by the search engine. Or I may just suck at searching with the right keywords, but I've been unable to find information about this. Perhaps somebody can elaborate.

I would to like to create a function, included in the header of an HTML page, which calls functions which are defined later, in <script /> tags farther down in the body of the page (I understand this is a terrible idea, but it is a constraint that I have to work with). Unfortunately, the intuitive way (but inherently impossible) of doing this doesn't work:
Expand|Select|Wrap|Line Numbers
  1. function myEarlyFunction() {
  2.   if(typeof myLateFunction != 'undefined') {
  3.     myLateFunction();
  4.   }
  5. }
  6. ...
  7. ...
  8. ...
  9. <script type="text/javascript">
  10. function myLateFunction() {
  11.   //do stuff
  12. }
  13. </script>
Is there a way to do this that...well, works? And if so, how?

Thanks so much!
Try this it will work insha-Allah

Expand|Select|Wrap|Line Numbers
  1. function myEarlyFunction() {
  2. if( (typeof myLateFunction != 'undefined') && (typeof myLateFunction == 'function') )
  3.     myLateFunction();
  4.   }
  5. }
  6.  
Aug 15 '07 #2
beatTheDevil
16 New Member
Try this it will work insha-Allah

Expand|Select|Wrap|Line Numbers
  1. function myEarlyFunction() {
  2. if( (typeof myLateFunction != 'undefined') && (typeof myLateFunction == 'function') )
  3.     myLateFunction();
  4.   }
  5. }
  6.  
This doesn't work. The problem is that, at the time of parsing, the JavaScript interpreter can find no definition of "myLateFunction " and so this test is never even considered. Try it. Just make an HTML file out of this and see what happens...

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function earlyFunction() {
  3.     if(typeof lateFunction != 'undefined' && typeof lateFunction == 'function') {
  4.         lateFunction();
  5.     }
  6.  
  7. }
  8. </script>
  9.  
  10. Holy crap it's de text. Lol.
  11.  
  12. <script type="text/javascript">
  13. function lateFunction() {
  14.     alert("HOLLAH!");
  15. }
  16. </script>
  17.  
  18. It's more text.
  19.  
  20. <script type="text/javascript">
  21.  
  22. earlyFunction();
  23.  
  24. </script>
What I'm looking for is some other mechanism for _dynamically_ (i.e. at RUNTIME) doing function lookups and allowing this sort of behavior. Suggestions? Thanks again!
Aug 15 '07 #3
beatTheDevil
16 New Member
I apologize. I'm an idiot. I had a typo in my sample file. Misaw is right; this does, in fact work. However, you don't need the (typeof ___ == "function") . Simply conditionalizin g on undefined works just fine.
Aug 15 '07 #4
hirakchatterjee
1 New Member
Try this...

Put the function below within the "<head></head>" tags


<script type="text/javascript">
<!--
function addEvent(elemen tObject, eventName, functionObject)
{
if(document.add EventListener)
elementObject.a ddEventListener (eventName, functionObject, false);
else
if(document.att achEvent)
elementObject.a ttachEvent("on" + eventName, functionObject) ;
}

function earlyFucntion() {

if(typeof myLateFunction != 'undefined') {

myLateFunction( );
}
}

addEvent(window ,'load',earlyFu cntion);

//-->
</script>



then call your
<script type="text/javascript">
<!--
function myLateFunction( ){
alert("fire");
}
//-->
</script>
wherever in the document you want to...
Aug 23 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
3530
by: Olaf Meding | last post by:
Is there a way to find out if I am using early or late binding given the reference ("excel" in the example below) returned by Dispatch()? >>> import win32com.client >>> excel = win32com.client.Dispatch('Excel.Application') Thanks much for your help. Olaf
21
3978
by: Mike MacSween | last post by:
Had some trouble with Word automation. Sorted it, in the process thought I would try late binding. Some people reccomend it. So this: ********************************************************* Public Sub MailMerge(strQuery As String, strTemplate As String) snip Dim doc As Word.Document Dim wrdApp As Word.Application snip Set wrdApp = New Word.Application
14
4312
by: Composer | last post by:
I've read many postings about the problem of Access.References.IsBroken and the consensus seems to be that late binding is the cure-all. I have a very complex Access application that needs hundreds of lines of code to format a Word document in a very specific way. Because my clients have various versions of Word, the problem of broken references comes up. I wish Microsoft had implemented a reasonable solution, so that VBA could do...
9
10430
by: Zlatko Matiæ | last post by:
I was reading about late binding, but I'm not completely sure what is to be done in order to adjust code to late binding... For example, I'm not sure if this is correct: early binding: Dim ws As DAO.Workspace Dim db As DAO.Database Dim qdf As DAO.QueryDef Dim rs As DAO.Recordset
7
6529
by: Entwickler | last post by:
hello, i have the following problem. i want to write a code that enables the user to call functions from a unmanaged code .dll at running time . so i have to use the late binding . i tried the following code but it doesn't walk : Assembly assemblyInstance = Assembly.LoadFrom(@"C:\myData\UnmanagedCode.dll"); i don't know if somebody can help me . Regards,
9
2003
by: Miro | last post by:
VB 2003 and Im still new to vb, so i hope i can explain this as best I can. I have a variable defined as such: ( simple example ) Dim AVariableOfSorts(,) As Object = _ { _ {"Last", "String", 20}, _ {"First", "String", 20} _ }
2
3718
by: kogrover | last post by:
ISSUE: COM Excel Sort works with Early Binding, but not Late Binding, but py2exe only does Late Binding I have code similar to this (type from notes, so there may be a typo...) import win32com.client xl = win32com.client.Dispatch("Excel.Application") xl.Visible = False xl.ScreenUpdating = False
5
12051
by: markww | last post by:
Hi, Can someone explain to me what static and dynamic dispatch are and what the difference is between the two? Do this even occurr in C++? Thanks
26
2815
by: Aaron \Castironpi\ Brady | last post by:
Hello all, To me, this is a somewhat unintuitive behavior. I want to discuss the parts of it I don't understand. .... f= lambda: n .... 9 9
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9067
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.