java - Reload an <img src =<%= function() %> > in jsp -
hi have query regarding image reload in jsp page
my code :
<!doctype html> <html lang="en"> <head> <!-- script auto refresh of graphs 5 seconds interval --> <script type="text/javascript"> var load, generation, store, ep,transaction,battery; window.onload = function() { transaction = document.getelementbyid("transaction"); setinterval(function() { transaction.src = transaction.src.replace(/\?.*/, function() { return '?' + new date() }) }, 1000) } </script> <%! public string transactionfunc(){ system.out.println("---------transactionfunc------------"); string name=psmapp.getinstance().notif.gettransaction(); string src = "../stylesheet/images/energydeficit256.png"; if(name!=null && name.equals("buy")){ src = "../stylesheet/images/energydeficit256.png"; } else if(name!=null && name.equals("sell")){ src = "../stylesheet/images/energysurplus256.png"; } return src; } %> <div class="col-md-4"> <img src=<%=transactionfunc() %>id="transaction"> </div> </body> </html>
when trying reload image using javascript above, not calling transactionfunc() jsp function (this function accesses java class , gets updated value of source of <img>
tag. how resolve issue transactionfunction() gets called every time image reloaded?
you trying call jsp method in javascript.
reason jsp files compiled .class during compilation , javascript scripts executed on client side.
either convert java code javascript or put java code in java file. i.e. servlet or jsp call method ajax , set src
in success block of ajax.
or
instead of java code use javascript function conditions. might little changes per code , requirement.
function transactionfunc(){ var name='<%= psmapp.getinstance().notif.gettransaction()%>'; var src = "../stylesheet/images/energydeficit256.png"; if(name !=null && name=="buy"){ src = "../stylesheet/images/energydeficit256.png"; }else if(name!=null && name.equals("sell")){ src = "../stylesheet/images/energysurplus256.png"; } return src; }
Comments
Post a Comment