• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calling functions in .js file

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic