Results 1 to 6 of 6

Thread: adding unique link to each timed banner

  1. #1
    Join Date
    Jun 2012

    adding unique link to each timed banner


    I snarfed a java script online that did as advertised: Allowed pics to be displayed in a rotating manner based on a time
    value you set in the script. I don't know if I can attach the script (this is my first time trying) so I will paste it here. It's fairly
    short. I've done a little bit of C and PHP programming, but I am a weekend warrior. Looking for a pro to help out. Here goes:

    var bannerImg = new Array();
    // Enter the names of the images below
    bannerImg[0]="/images/cover one.png";
    bannerImg[1]="/images/cover two.png";
    bannerImg[2]="/images/cover three.png";

    var newBanner = 0;
    var totalBan = bannerImg.length;

    function cycleBan() {
    if (newBanner == totalBan) {
    newBanner = 0;
    // set the time below for length of image display
    // i.e., "4*1000" is 4 seconds
    setTimeout("cycleBan()", 4*1000);

    Most of it seems pretty straight forward:

    1) create an array then stuff your pics in it.
    2) set a couple of variables, one that equals 0 and the other the length of the array
    3) create a function and begin it with 'For Loop' with a counter so you don't count past the length of the array (all very C'ish so far)

    The next line starts to get a little weird for someone who doesn't work with javascript. It's assigning document.banner.src equal to what's in the zero position of the array. Then it wait's for 4 seconds and exits the function. Then the line window.onload=cycleBan; not sure what that means. I can only deduce that since it has .onload in it, that it loads up the pic and displays it? Then the script counts through and displays all the pics.

    But what really weirds me out is what I have to put in the cell to make it all work:

    <TD width=342 height=410 border=0 valign=top align=center><img src="image_jss.gif" name="banner"border=0><TD>

    Img src="image_jss.gif" what's that all about? I mean, it works and all, put my pics are .png and you have to have name=banner too or it won't work. Anyway, Is there a way that when one of the pics is displayed on the webpage a link can be assigned to it? then when the pic changes, a different link can be assigned? And so on? I could have asked all this without going through the code, but I thought it would be a good chance to learn a little more about how javascript works.

    much appreciated,

  2. #2
    Join Date
    Mar 2007
    I would use an array of objects.

    YourArrayData = [{src:"/images/cover one.png",href:"link1.html",duration:4000},
    {src:"/images/cover two.png",href:"link2.html",duration:8000}, ...];
    You then have to rotate the array rather than keep count and save on a variable.

    var tmp = YourArrayData.shift(); // remove first element
    YourArrayData.push( tmp ); // put back on array
    your tmp variable is now an object containing the data you need...

    tmp.src can be used to set the src element of an image tag <img
    tmp.href can be used directly to set the href tag of the link <a
    tmp.duration can be used to set the time out fir the display allowing you to have different timings for each banner.

    So you would need something like
    <a id="bannerLink" href="#"><img id="bannerImg" src="image_jss.gif" name="banner" border=0></a> 
    bImg = document.getElementById("bannerImg");
    bLink = document.getElementById("bannerLink");
    bImh.src = tmp.src;
    bLink.href = tmp.href;
    You get the picture...

    You can add other information in the object data and apply it to the <a and <img tags such as title and even functions.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Jun 2012
    Whoa, looks like this is a little bit above my pay grade. I may have to stare at it for a few days.


  4. #4
    Join Date
    Mar 2007
    Its fairly simple when you look at what the data is.

    1. You have an array
    2. The array has "Objects" which is a fancy array that allows you to name stuff
    3. you don't have a loop counter and no checking of the array length to contend with because you are not moving a "pointer", you are moving the data while grabbing a copy of the current data from the array, that data is the object.
    4. you access the data by its "property" .src and .href
    5. the objects in the array can contain more elements that describe other items you want to have set like the image tags title.
    6. you then set a timer function to call the update routine that starts the whole process of grabbing an object from the array, stuffing it back in to the array...
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  5. #5
    Join Date
    Mar 2007
    If it helps... you could use something like this which is out of my head and untested but in theory would work if you changed the names of target elements...

    banner = {
                this.targetImg = document.getElementById("theTargetImage");
                this.targetLnk = document.getElementById("theTargetLink");
         rotateArray = function(){
            var tmp = this.img.shift();
            this.img.push( tmp );
            return tmp;
                var i = this.rotateArray();
                this.targetImg.src = i.src;
                this.targetLnk.href = "/images/"+i.href;
                this.auto = setTimeout("banner.update();",i.duration);
    // now add some data
    banner.addData({src:"cover one.png", href:"link1.html", duration:4000});
    banner.addData({src:"cover two.png", href:"link2.html", duration:8000});
    banner.addData({src:"cover three.png", href:"link3.html", duration:5000});
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  6. #6
    Join Date
    Mar 2007
    Maybe it would help to explain so you understand the function.

    banner is a JSON variable that contains functions and variables.
    When the script is loaded, the value of banner.auto is set and that is a setInterval timer that is called every 2 seconds and the function that is called is banner.init()

    the init() function is set to check if the array that holds the images has any data, if it has then it cleared the interval timer and grabs the identity of the two elements you will be setting with the image and link data. The function calls the function update() which should be inside the if(){} conditional but typo made if outside of it...

    Once the update function is called the data in the array is obtained, these are objects.

    The array removes the first element in the array in to a variable tmp then pushes it on to the back of the array and then returns an object, eg {src:"cover one.png", href:"link1.html", duration:4000}

    This is returned to the variable i, this means that I now has properties that can be accessed by using the property name like i.src which is the value in that returned object.

    The target properties are now set with this information, your image chages and the link values of the click link also change.

    The process is then called again by a setTimeout event which has its duration set by the current object and the whole process starts again and runs while you have an array with images in.

    IMHO a routine like this is better because you can add features, extend the object data to include other elements.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09679 seconds
  • Memory Usage 2,912KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (4)bbcode_code
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (6)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (6)postbit
  • (6)postbit_onlinestatus
  • (6)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates