Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
33
rated 0 times [  40] [ 7]  / answers: 1 / hits: 33238  / 13 Years ago, fri, july 22, 2011, 12:00:00

I'm trying to call javascript function from a href . the function has a parameter which will be retrieved by the eval function . But some error occurs .



script:



function rate(id) {

// do something
}


the a tag that will call the function:



<a href=javascript:rate( + <%#Eval(ID)%> + ) >rate</a>


What am I missing ?


More From » asp.net

 Answers
96

You shouldn't be doing it like this, but the issue you're currently up against is probably is your quoting/concatenating.



If <%#Eval(ID)%> simply produces an INT, this should work:



<a href=javascript:rate( <%#Eval(ID)%> ) >rate</a>


If it's a string,



<a href=javascript:rate( '<%#Eval(ID)%>' ) >rate</a>


should do it for you, although you need to handle the case of <%#Eval(ID)%> producing anything with a single quote in it.



A Lesson:



I say you shouldn't be doing it like this because the javascript pseudo protocol (javascript:) is defunct and improper. At worst you should be using an onclick which returns false. Ideally you'd be assigning the event programatically and preventing the event object's default action.


[#91053] Thursday, July 21, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
willieelisham

Total Points: 201
Total Questions: 108
Total Answers: 106

Location: Zambia
Member since Sat, Oct 31, 2020
4 Years ago
;