• 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 javascript function in js file from JSP

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I am trying to call javascript function from jsp file

Following is JSP code :

<script type ="text/javascript" src = "/staic/javascript/Jsrp.js" >
.
.
<a href = javascript:jsCalled()> Call JS Function </a>; </br>


and following js code in Jsrp.js file located in

function jsCalled()
{
alert('jsCalled function called');
}

but when I click (Call JS Function) I get js error as jsCalled is not defined.

Can any one help?
 
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In src attribute of script tag give whole path of file(including context path).
by this it will work(i thing so)
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you understand how relative URL's work?

Originally posted by Milind Manjrekar:
<script type ="text/javascript" src = "/staic/javascript/Jsrp.js" >


Relative URL's are relative to the current context.
The leading slash will bring you to the domain root.

Verify if it is right. Also, check for any typos, "staic" isn't a valid English word.
 
Mil Mnj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it was typo error while posting here

Correct code is:
<script type ="text/javascript" src = "/static/javascript/Jsrp.js" >

I tried removing leading slash. However, I still get the same error jsCalled() is not defined.

I think this error needs setting context path, not sure though.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Milind Manjrekar:
it was typo error while posting here


Typo? Why didn't you just copypaste it? Changing code because of posting it at a forum is a bad idea.


Correct code is:
<script type ="text/javascript" src = "/static/javascript/Jsrp.js" >

I tried removing leading slash. However, I still get the same error jsCalled() is not defined.


Tried? So you don't understand how relative URL's work?
Anyway, what's the absolute URL with which you requested the JSP file? (that URL which you see in the browser address bar). Also, what is the absolute URL with which you can call the .js file independently? Once you know that, you should be able to extract the right relative URL of the .js file from it.


I think this error needs setting context path, not sure though.


This sentence makes no sense.
 
Mil Mnj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya, typo error while posting here, not a big issue , I dont know how t got missing , anyway I know how relative URLs work, and have resolved the issue.

Thanks
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What was the problem then?
 
Mil Mnj
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya, typo error while posting here, not a big issue , I don't know how t got missing , anyway I know how relative URLs work, and have resolved the issue.

Problem was some other JSP comment was giving problem for JS function execution. Now, please don't ask why did you type JSP comment in JS file(because I wanted to test the same).
Anyway, thanks for your help

Thanks
 
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
Typos are a big deal. If you waste people's time following "red herrings" they will soon learn to ignore your questions. Please read this for more information.
 
Ranch Hand
Posts: 494
Eclipse IDE Postgres Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first, try to call local javascript function (javascript function written in the same page);



this is used just for ensure that the javascript function could be called in JSP pages..
and then try to move that function to certain javascript file..
and write your path of file correctly..

Please correct me if i'm wrong...
 
Ranch Hand
Posts: 114
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mil Mnj said 'Problem was some other JSP comment was giving problem for JS function execution'.
Could someone explain this to me ?
 
Bear Bibeault
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
I have no idea what was meant by that.

Is there a specific problem you are having? If so, start a new topic describing your issue.
 
Why is the word "abbreviation" so long? And this ad is so short?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic