File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes calling functions in .js file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "calling functions in .js file" Watch "calling functions in .js file" New topic
Author

calling functions in .js file

Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
How can I write code in external JavaScript (in .js file)....can I write this in js file?

document.getElementById("packageBlock").style.display = 'block';

or,

<%
packSummary.deleteDestinations(request.getParameter("deleted"));
%>

if not, how this can be implemented?
I wrote it in js file....not able to call functions from HTML form...

Please help me out...

Regards,


Debashree Halder<br />SCJP 1.5<br />Washington DC
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
That absolutely possible. Sourcing a JS file in a web page has the same effect as including the JS code directly in the page.

Your second line of code looks like JSP, so I'm not sure how that relates to JS?


Ping & DNS - my free Android networking tools app
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
Yes, it's actually a jsp page that uses some javaScript functions having some scriptlets inside the functions....how can I use this?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Just to be clear: JSP is a server-side technology, while JavaScript is a client-side technology. JSP can't access JS, and vice-versa.
What JSP can do is generate JS code in the web page it generates, and that JS code can call other JS code that gets included in the web page from an external JS file.
If this doesn't address your problem, describe in more detail what you're stuck with.
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
Actually I'm stuck at the initial stage.Presently I'm having Javascript code inside script tag of html, which I need to shift in .js file....

Say there is a function:

function enableFields(){

document.getElementById("editButton").style.display = 'none';
document.getElementById("packageBlock").style.display = 'block';
document.getElementById("packageLabels").style.display = 'none';
document.getElementById("selectorDetails").style.display = 'block';
document.getElementById("selectorLabels").style.display = 'none';
}

it work fine in HTML file...if I shift it into .js file...it says "Object Expected" when I try to call the function.

please first and foremost help me to solve this problem...

Regards,
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
If it does not work when you move it from inline to the js file it usually means three things. Not linking the external file correctly, you put script tags inside of the external js file, or you did a bad copy and paste job and cut off some brackets or letters.

Eric
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61095
    
  66

JSP directives, scriptlets, actions or any other server-side mechanisms will not be interpreted when resident in a .js file.

At least, not by default.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
Can I write some JavaScript functions inside JSP and few functions outside in JS file?

like this...

script language="javascript" src="JS/hpop.js"
script

script language="JavaScript"

function call(){

//somecode
}

script
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
As Eric pointed out earlier, you need to remove the <script> tags when you put JS code inside in an external file. Then it will work.
 
wood burning stoves
 
subject: calling functions in .js file