This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Calling function from a separate HTML file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Calling function from a separate HTML file" Watch "Calling function from a separate HTML file" New topic
Author

Calling function from a separate HTML file

Lubosh Bazant
Greenhorn

Joined: Jan 15, 2002
Posts: 14
I am trying to call a method which is coded in a separate HTML
document which is in a different directory location.
for example
o nclick="document.doSomething()"
if i do this then I get an error that middle is unidentied. Fair
enough, the document.html document is not in the same directory as the
document from which I am calling the function, thus it can't find the
method. However if I point the method to the right directory, it
gives me a syntax error: see below
o nclick="../../document.doSomething()"
This in turn gives me a syntax error.
In other words: How do I call a function which is coded in a separate
HTML document which is not in the same directory as the directory
where I call the function from?
PS: O nclick really means onclick but the server was giving me problems in posting the message.
[ February 12, 2003: Message edited by: Lubosh Bazant ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Regardless of whether the HTML file containing the function is in the same directory or different, you cannot call a function in a page that is not loaded into the browser.
If you need to share functions between pages, the best practice is to place the function(s) in a separate file (conventionally named with the .js suffix), and use the src attribute of the <script> tag to include it on pages that need it.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Lubosh Bazant
Greenhorn

Joined: Jan 15, 2002
Posts: 14
Bear,
Thank you for your prompt answer. I forgot to mention that the page where the code is written is loaded into the browser. There are 2 frames. The HTML page on the top frame has the code for the function I am calling in the lower frame. The top frame is always present and loaded. The problem is that the phisical location of the HTML document of the upper frame is not in the same directory as the HTML document for the lower frame. Thus I have to use the "../../" notation to redirect it to the correct directory to find the HTML document of the upper frame (2 locations up the directory structure) But this notation gives me a syntax error. Is there a way around this.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
actually it is easier then that:

on_click="parent.FrameName.doSomething()"
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

Eric is correct -- the means to get at the function depends upon the window hierarchy of the loaded documents, and NOT the directory structure from which they were served (which is meaningless in this case).
hth,
bear
Lubosh Bazant
Greenhorn

Joined: Jan 15, 2002
Posts: 14
Thanks for the help it workes now. I have one last follow up question. What the function
does is to add an item to an array that is delared an initialized in the top frame, which
is always present and loaded. As one of you suggested, I could export the code and array initialization from
the top frame HTML document to a .js file and have it coded and inicialized there. My
question is, if I initialize an object in a .js file will I be able to share the data from
the object as I surfe from one page to another in the lower frame? The reason why it works
now is because the object is loaded on the top frame and it stays there even if I surf the
lower frame. Can I achieve the same thing through .js strategy. In other words what determines the lifespan of an object in .js?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60810
    
  65

When you include a .js file on a page, it acts just as if you had the JavaScript hard-coded onto the page.
(A little expirementation on your part would have told you that).
hth,
bear
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling function from a separate HTML file
 
Similar Threads
Controlling applet from html code
Calling Word macro from java getting read only popup
onClick function call with a scriptlet parameter
select tag in struts
Calling Javascript functions from an applet