Hi guys, I’m completely a noob with Javascript or scripting in general. I wanted to find a simple graph script and came across this script. It works great but I want to add link URL to the title of each bar (ie CNN) how do I do this?
Thanks!
var graphimageP="poll.gif"
//DEFINE GRAPH VALUES [Item name, Percentage value]
var graphv=new Array()
graphv[0]=["CNN","28%"]
graphv[1]=["MSNBC","36%"]
graphv[2]=["ABC News","11%"]
graphv[3]=["BBC News","25%"]
//YOU CAN DEFINE MULTIPLE GRAPHS, eg:
//var graphz=new Array()
function graphitP(g,gwidth){
outputP='<table border="0" cellspacing="0" cellpadding="0">'
for (i=0;i<g.length;i++){
calwidthP=gwidth*(parseInt(g[i][1])/100)
outputP+='<tr><td>'+g[i][0]+' </td><td><img src="'+graphimageP+'" width="'+calwidthP+'" height="10"> '+g[i][1]+'</td></tr>'
}
outputP+='</table>'
document.write(outputP)
}
//CALL GRAPHIT FUNCTION
//graphitP(NAME OF GRAPH ARRAY, MAXIMUM WIDTH IN PIXELS)
graphitP(graphv,200)
</script>
I looked at it, and decided I would try to add to where the array got defined.
However is that the smartest way? Having HTML in a string like that? Would it be smarter to actually create an anchor with JS instead? If so, that would be kinda complicated, wouldn’t it?
Oooh… I didn’t think of adding it to the second array like that (I was thinking of adding it as an HTML string which isn’t something I feel can be Good).
…Isn’t innerHTML frowned upon by the same folks who frown upon document.write?
No, not really. innerHTML is seen as Less Bad than document.write. DOM methods would be best, but I don’t really feel like rewriting the above code. There is some argument as to whether innerHTML is actually better than DOM methods, because it’s faster (especially for big loops or giant chunks of HTML) and less verbose. jQuery uses it quite a lot.
That said, I like the DOM methods because they help build the tree of what I’m doing in my head. And of course it can do things that innerHTML can’t do without messy string manipulations.
[ot]Yes… I saw a video where Crockford stated that innerHTML was created by MS because… there was nothing like it in the DOM specs and should’ve been created so it’s no wonder the other browsers adopted it… and that it’s not Good but sometimes in some situations just makes more sense due to the speed issues (so long as one is also aware of the security issues), and that the worst thing about it might be that it’s proprietary.
I’ve also seen some other things that MS really got more-right, and it would be nice if the W3 specs were revised to include/revise these.[/ot]
Microsoft-bashing is a very popular pastime, but they do get some things right. For example, they invented the XMLHttpRequest that everyone loves so much nowadays, they just kept it as an ActiveX object for a bit too long. Same with clientHeight and offsetHeight - introduced by IE and later added by the others. Very useful properties and not in any standard.
innerHTML is a part of the HTML5 standard, so now that it’s a part of a standard there’s no doubting its legitimacy!