Open URL when a thumbnail is clicked
Actionscript 3, Flash Components, Tutorials January 21st, 2008Adding this feature to the Thumbnail Scroller Component is pretty easy but requires some knowledge on working with XML. In this tutorial I will show you how to do this using Actionscript 3.
1. First we need to add another attribute to the XML file. I will call this new attribute “urlPath” but you can use any name. I only want to add this attribute for the first 3 thumbnails.
My XML file will now look like this:
-
<images>
-
<image path="thumbnails/image0.jpg" urlPath="http://www.adobe.com" />
-
<image path="thumbnails/image1.jpg" urlPath="http://www.google.com" />
-
<image path="thumbnails/image2.jpg" urlPath="http://www.yahoo.com" />
-
<image path="thumbnails/image3.jpg" />
-
<image path="thumbnails/image4.jpg" />
-
<image path="thumbnails/image5.jpg" />
-
<image path="thumbnails/image6.jpg" />
-
<image path="thumbnails/image7.jpg" />
-
<image path="thumbnails/image8.jpg" />
-
<image path="thumbnails/image9.jpg" />
-
</images>
2. Add the needed code for the navigation:
-
import flash.events.MouseEvent;
-
-
left.addEventListener(MouseEvent.CLICK, leftHandler);
-
right.addEventListener(MouseEvent.CLICK, rightHandler);
-
-
function leftHandler(event:MouseEvent){
-
myScroller.moveLeft();
-
}
-
-
function rightHandler(event:MouseEvent){
-
myScroller.moveRight();
-
}
3. Now, we need to handle the XML file. At first, this might seem a little complicated if you haven’t worked with XML in AS3 but it’s really not that difficult. It’s just that AS3 has a different way of working with external XML files.
-
import flash.events.Event;
-
import flash.net.URLLoader;
-
import flash.net.URLRequest;
-
-
var urlRequest:URLRequest = new URLRequest("thumbnails.xml");
-
var urlLoader:URLLoader = new URLLoader(urlRequest);
-
var urlArray:Array = new Array();
-
-
urlLoader.addEventListener(Event.COMPLETE, onXmlComplete);
-
-
function onXmlComplete(event:Event){
-
var xmlList:XMLList;
-
var nr:int;
-
var xml:XML = new XML(event.target.data);
-
xmlList = xml.children();
-
nr = xmlList.length();
-
for(var i:int = 0; i < nr; i++){
-
urlArray[i] = xmlList[i].attribute("urlPath");
-
}
-
}
-
4. Open an URL when a thumbnail is clicked:
-
import com.flashotaku.thumbnailscroller.events.ThumbnailEvent;
-
import flash.net.navigateToURL;
-
-
myScroller.addEventListener(ThumbnailEvent.CLICK, openUrl);
-
-
function openUrl(event:ThumbnailEvent){
-
var nr:int = event.item.id;
-
if(urlArray[nr]!=undefined){
-
var urlRequest:URLRequest = new URLRequest(urlArray[nr]);
-
navigateToURL(urlRequest, "_blank");
-
}
-
}
Support
As of 11 March 2009, support is no longer provided for the free components. If you need support, please purchase the commercial version of the components.
January 25th, 2008 at 1:44 am
Ok, you are awesome!
Thank you! Thank you! Thank you!
February 2nd, 2008 at 5:23 am
Hi how are you? Excellent work. i just have a question about the xml side of things. can this be made to work with any xml structure? lets say i had an xml
this is only an example. can this component be customized to work with such a simple structure?
February 2nd, 2008 at 9:51 am
Hi
I can’t see your xml example…
The required xml structure for reading the path to each image is this:
<images><img path="images/image0.jpg" />
<img path="images/image1.jpg" />
<img path="images/image2.jpg" />
<img path="images/image3.jpg" />
<img path="images/image4.jpg" />
<img path="images/image5.jpg" />
<img path="images/image6.jpg" />
<img path="images/image7.jpg" />
<img path="images/image8.jpg" />
<img path="images/image9.jpg" />
</images>
and the only required keyword is ‘path’. You can change ‘images’ and ‘img’ to ‘thumbnails’ and ‘thumbnail’ if you want.
March 24th, 2008 at 8:42 pm
Hello, I found your Blog by Google search, I’m so happy to find it , it’s wonderful work Congratulations!
Would you please explain how to Open URL when a thumbnail is clicked using AS2 ?
Many Thanks!
March 24th, 2008 at 9:48 pm
Great Work!
March 24th, 2008 at 10:13 pm
Hi,
I’ve made an example http://flashotaku.com/files/scroller_as2.zip
March 26th, 2008 at 12:47 pm
Just some comments on the mail before.
Can se some body is having a component where they do something like this, though im still a little blank how to incorporate that into your component
Open the xml file and write your frame label in the “data” attribute
Write the code to play to that frame:
function clickHandler(dat, lbl) {
// handler function receives “data” as string and “label” as string
var label:String = dat;
some_mc.gotoAndPlay(label);
}
April 30th, 2008 at 4:34 pm
is there a way to combine the xml info for the thumbnail scroller and the slide component in one file?
ie.
or
thanks for your help, you’ve made some great components!
paul
April 30th, 2008 at 4:40 pm
wordpress hide my xml. let me try again
can we have one xml file for the slideshow component and the thumbnail scroller?
[xml]
[images]
[pic path='link']
[pic path='link']
[pic path='link']
[images]
[thumbs]
[pic path='link']
[pic path='link']
[pic path='link']
[thumbs]
[/xml]
April 30th, 2008 at 5:38 pm
You could use the same XML for the slideshow and for the scroller only if you want to load the same image for the thumbnail and for the slide.
April 30th, 2008 at 6:17 pm
ok. i was hoping to have separate files for the thumbnails to save loading time. thanks
July 17th, 2008 at 11:53 pm
I was wondering do you have to make an attribute of the url link?
July 18th, 2008 at 10:34 am
It’s not necessary to keep the URLs as attributes. You could simply store them into an array if you want, but keeping them in the XML file makes it easier to update.
August 12th, 2008 at 2:08 am
FlashOtaku, first of all, excellent work. When I try to reproduce what you did and I play the animation (after clicking the right arrow, I get the following error. Please help! (I have imported the ThumbnailScroller from your “Flash Components” section)
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at com.flashotaku.thumbnailscroller::ThumbnailScroller/moveRight()
at myTest_fla::MainTimeline/rightHandler()
August 12th, 2008 at 12:11 pm
It’s difficult to say what is wrong unless I see your code, so please post some code or you can send me your file to check it.
September 3rd, 2008 at 12:25 pm
Hey Otaku
I posted about this on the slideshow component page but perhaps it is more relevant here. I have adapted your AS3 slideshow component but I want each slide to be a link like with these thumbnails, preferably to open in the same window. I have changed the .XML file and can link the slides but it appears I need some action script for the links to open in the same window. I’m assuming the default URL target is _blank. Any ideas?
Many Thanks
September 3rd, 2008 at 1:27 pm
Hy Will,
If you want to open the page in the same window, use “_self” instead of “_blank”.
September 16th, 2008 at 1:04 pm
Hi Again!
This is my current Action Script.
I want to open my URL’s in the same Window.
Could you please complete the Action Script with:
NavigateToUrl “_self”
Idont know where to put it.
AC:
import com.flashotaku.slideshow.events.SlideshowEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
var myTimer:Timer = new Timer(6000);
mySlideshow.addEventListener(SlideshowEvent.TRANSITION_COMPLETE, transitionCompleteHandler);
myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
function transitionCompleteHandler(event:SlideshowEvent){
myTimer.start();
}
function timerHandler(event:TimerEvent){
myTimer.stop();
mySlideshow.nextImage();
}
September 16th, 2008 at 2:55 pm
Hi Daniel,
The code you have there is only for the auto slideshow.
Anyway, here a simple example: http://flashotaku.com/files/slideshow_url_as3.zip
December 22nd, 2008 at 12:17 pm
hi and thanks
this solved many problems for me.
One simple question.
How can I make the pointer to be a hand when over a link?
is this an xml or an actionscript characteristic?
thanks in advance
December 22nd, 2008 at 2:05 pm
Hi lefteris,
You just need to set the handCursor property to true. You can do this in the Component Inspector or using Actionscript : myScroller.handCursor = true;
December 22nd, 2008 at 3:51 pm
[...] a previous tutorial, I’ve described how you can open an URL when a thumbnail is clicked. It was not difficult to [...]
December 27th, 2008 at 5:06 am
Hi,
I have a similar question to Anders Daugaard but I don’t believe he’s gotten an answer yet. My question is this: How can you use XML such that when you click on an XML loaded image, you are directed to a Frame Label internally (as opposed to an external URL). I’m not concerned with an outside weblink. Just directing the user to a frame, in an .fla with a specific frame label.
I’ve seen the following technique:
This example is going to assume you have a texfield on stage named “tfield” that the content is being loaded into. It also assumes that your target frame label is “fm1”.
This is what the XML node will look like;
The first frame
Now you need this code somewhere;
tfield.addEventListener(TextEvent.LINK, linkClicked);
function linkClicked(event:TextEvent):void{
trace(event.text) //This will trace out fm1
gotoAndPlay(event.text)//this will use fm1 as the frame label
}
But I haven’t seen any technique without using the ‘a’ link and the TextEvent.LINK event.
So is there one?
Thank you, ezFlash
December 29th, 2008 at 12:55 pm
Hi ezFlash,
You need to create additional attributes for the frames in the XML file and then you create an Array to store those values(for this you would have to parse the XML file again).
Here’s an example: http://flashotaku.com/files/scroller_frame_as3.zip
December 30th, 2008 at 3:03 am
Thank You FlashOtaku!! U Rock :))
May 15th, 2009 at 1:09 pm
[...] components. The answer is yes, you just need to parse the XML file again. Two weeks ago I made a tutorial on how you can add new elements to the Thumbnail Scroller component. It’s pretty easy to do [...]
June 2nd, 2009 at 4:44 pm
Is there a program that will merge the thumbnail with a code automatically so when you click the thumbnail it will take you to the site intended?
If yes! What is that program?
June 2nd, 2009 at 6:30 pm
No, you just need to specify the thumbnail’s link in the XML file and then add a little actionscript code inside the FLA file.
June 18th, 2009 at 7:55 pm
Hi Otaku
I would like to cover the thmbnais with a grey rectangle(alppha mode) when there is roll over of the mouse…
How do I do?
thanks
June 23rd, 2009 at 3:52 pm
Hi,
Take a look at this tutorial: http://flashotaku.com/blog/roll-over-effect-for-the-thumbnailscroller/