Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling function from a separate HTML file

 
Lubosh Bazant
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lubosh Bazant
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
actually it is easier then that:

on_click="parent.FrameName.doSomething()"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64982
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic