File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling javascript function in js file from JSP

 
Mil Mnj
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Shailesh Narkhede
Ranch Hand
Posts: 368
  • 0
  • Mark post as helpful
  • send pies
  • 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)
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What was the problem then?
 
Mil Mnj
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Leonardo Carreira
Ranch Hand
Posts: 489
Eclipse IDE Java Postgres Database
  • 0
  • Mark post as helpful
  • send pies
  • 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...
 
Tiya Khambadkone
Ranch Hand
Posts: 67
1
  • 0
  • Mark post as helpful
  • send pies
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64185
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic