View Full Version : Dynamically adding URLs to buttons
vipergtsrz
06-14-2006, 08:44 PM
I have an XML file that has a large group of buttons. (ex below)
<unitedstates>
<state name="WA">
<enabled>1</enabled>
<bordercolor>3F3F3F</bordercolor>
<color>336699</color>
<url>http://www.hotmail.com</url>
</state>
<state name="OR">
<enabled>1</enabled>
<bordercolor>3F3F3F</bordercolor>
<color>336699</color>
<url>http://www.gmail.com</url>
</state>
</unitedstates>
I want to be able to dynamically have each of those buttons go to the URL I specified in the <url></url> tag.
This is the code that I am trying to do as I loop through the XML file, but it's assigning it onrelease. I want to assign it at the beginning.
for(var i = 0; i < xmlDoc_xml.firstChild.childNodes.length; i++){
var strURL = xmlDoc_xml.firstChild.childNodes[i].childNodes[3].firstChild.nodeValue;
eval(stateName).onRelease = function(){
trace(strURL);
getURL(strURL, "_self");
}
}
the problem with this is that it assigns the last URL in the XML file to the every button I click on. So in this instance, the WA button AND the OR button will both go to gmail.com because that's the last one it was assigned to.
How do I add the right URL to the right button in that loop and have it stick?
Thanks.
silentweed
07-20-2006, 10:21 PM
The button will pick up the last value of i whatever it was when the loop finished executing..the waqy around this is to assign a dynamic property to a button that will record the value of i when its on release is defined..as an example suppose u have buttons mc0, mc1, ..mc5 then the following will tell u which button was pressed example:
for(var i:Number=0 ; i<6; i++){
_root["mc"+i].id = i;
_root["mc"+i].onRelease = function(){
trace("you pressed button " + this.id);
}
}
Apply the same principle to your code...
csticky
07-21-2006, 07:01 PM
ya, that seems to pass the variable, but it's still not pulling the correct link, only the last link in the node.
here the actionscript:
Function loadContent() {
for (var i:Number=0; i<RootNode.childNodes.length; i++){
this.attachMovie("news_detail_id" , "news_" + i+1 +"_mc" , this.getNextHighestDepth());
this["news_" + i+1 +"_mc"].linkBtn_mc.id = i;
this["news_" + i+1 +"_mc"]._y=(i)*(clpHeight);
var titleTxt:String = RootNode.childNodes[i].attributes.newsTitle;
var newsLinker:String = RootNode.childNodes[i].attributes.newsLink;
var newsLinkTitle:String = RootNode.childNodes[i].attributes.newsLinkTitle;
var newsDetail:String = RootNode.childNodes[i].firstChild.nodeValue;
this["news_" + i+1 +"_mc"].newsTitle_txt.text = titleTxt;
this["news_" + i+1 +"_mc"].newsCopy_txt.html = true;
this["news_" + i+1 +"_mc"].newsCopy_txt.htmlText = newsDetail;
this["news_" + i+1 +"_mc"].linkBtn_mc.newsLinkTitle_txt.text = newsLinkTitle;
this["news_" + i+1 +"_mc"].linkBtn_mc.onRollOver = function () {
this.gotoAndPlay(2);
}
this["news_" + i+1 +"_mc"].linkBtn_mc.onRollOut = function () {
this.gotoAndPlay(1);
}
this["news_" + i+1 +"_mc"].linkBtn_mc.onRelease = function () {
getURL(newsLinker+this.id);
//trace(this.id);
}
}
}
and here's the xml code
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<newsListing>
<news newsTitle = "News 1" newsLink = "http://www.aroundphilly.com" newsLinkTitle = "www.aroundphilly.com">
<![CDATA[This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool. This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool]]>
</news>
<news newsTitle = "News 2" newsLink = "http://www.aroundac.com" newsLinkTitle = "www.aroundac.com">
<![CDATA[This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool. This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool]]>
</news>
<news newsTitle = "News 3" newsLink = "http://www.heyphilly.com" newsLinkTitle = "www.heyphilly.com">
<![CDATA[This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool. This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool This is an event that happens at Mia in Atlantic City that is cool and rockin. You need to come to this event if you want to be cool]]>
</news>
</newsListing>
any help here would be great...we've been stuck on this for 2 days. thanks.
C.
Did you figure this out?
You're close to what you want to do. I'm not sure how you were trying to use the id in the code you posted though.
getURL(newsLinker+this.id);
You're just appending a number to the end of the same old URL here. Attaching a dynamic id property is the right way to go (at least that's what I'd do), but you need to utilize it differently. One solution would be to use the ID as an index to directly access the urls stored in the XML:
getURL(RootNode.childNodes[this.id].attributes.newsLink);
That'll point you in the right direction (Or at least towards something that works.)
On a side note, and this is personal preference, but when I'm generating names on the fly like you're doing here it seems convenient to store a reference to the clip rather than having flash resolve some wacky string every call. The attachMovie method helpfully returns a reference, so you could refer to this clip as my_mc or something rather than this["blahblah"+i+"_mc] over and over.
|
vBulletin® v3.8.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.