This week's book giveaways are in the Scala and Android forums.
We're giving away four copies each of Machine Learning Systems: Designs that scale and Xamarin in Action: Creating native cross-platform mobile apps and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

how does this code work  RSS feed

 
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found some code on SO that does what I want, but I don't understand how it works. Background: I have an iframe which will display a chart (JFreeChart) generated by a servlet. The user enters a date range which goes to another servlet which gets the dataset and puts it in the session. The chart generator grabs that dataset and creates the chart. I needed to refresh the iframe so it would build a new chart when the parameters are entered or new parameters are entered.

This may not be the best way to do things, but after two days of struggling with JFreeChart, this is what I came up with, based on the examples in their developer guide.

The first solution suggested was this:

In other words, setting the src attribute to itself to force a refresh. This didn't work for me. This does work:

But I don't understand how it works. If I leave out the "i" parameter, it doesn't work. So what is "i" and where does "val" get a value from?
 
Author and ninkuma
Marshal
Posts: 66677
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that's a weird one and not one of the browser vagaries that I've seen. You can set a breakpoint in the function to see what's bing passed to it (if anything) as the 2nd param.

And here's where I offer an alternative: nothing made me happier than when I stopped trying to do charting on the server and moved it to the client. You may find that switching to a client-side charting package such as HighCharts causes you to pull out less hair.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's called from the success function of an Ajax call. I tested it and "i" is zero (a default value?), and val is the src url of the iframe, but I don't know how it's getting that.

It's a weird one alright. It works, but I hate not knowing why it works.
 
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J. Kevin Robbins wrote:It's called from the success function of an Ajax call.



There is no Ajax call!


Read the documentation for http://api.jquery.com/attr/#attr-attributeName-function
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66677
167
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eric beat me to it.

It still doesn't solve the issue of why the first form didn't work for you, but I prefer the 2nd form in any case because it does not rely upon repetition of the selector (a vector for introducing errors).

[Aside: I'd still recommend looking into client-side charting. There are lots of advantages, including interactivity, simplification if the code, and elimination of the iframe.]
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Eric Pascarello wrote:
There is no Ajax call!


Sorry, I was trying to keep the code to a minimum since it was that one line that puzzled me.
 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bear Bibeault wrote:
[Aside: I'd still recommend looking into client-side charting. There are lots of advantages, including interactivity, simplification if the code, and elimination of the iframe.]


I never heard of HighCharts. I'm going to look at it. Thanks for that tip.
 
If you try to please everybody, your progress is limited by the noisiest fool. And this tiny ad:
Why should you try IntelliJ IDEA ?
https://coderanch.com/wiki/696337/IntelliJ-IDEA
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!