Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling functions in .js file

 
Debashree Halder
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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,
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Debashree Halder
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64700
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Debashree Halder
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic