www.webdeveloper.com
Results 1 to 3 of 3

Thread: JS Help for function

Hybrid View

  1. #1
    Join Date
    May 2009
    Posts
    5

    JS Help for function

    Maybe someone can help me!? I am not very experienced in javascript.
    The problem: I have two images which I us for different switching displays, the small one is s_filename.jpg and the large one is x_s_filename.jpg

    The script below is grabbing the filename prefix, but only the first letter, how can I manage it to get the first 3 or 2 letters like x_s

    function sizeName(imgsize){
    var size_name

    switch (imgsize)
    {
    case 1 :
    size_name = 'x_s';
    break;
    default : // 0
    size_name = 's';
    }
    return size_name;
    }
    I appreciate any help.
    Last edited by omar1000; 05-03-2009 at 07:43 AM.

  2. #2
    Join Date
    Aug 2007
    Posts
    3,767
    No, what that code does very inefficiently, is return "s" if you call it as sizeName(1), and "x_s" if you call it as sizeName(1). So you showed us the wrong part of the code.

    And from a programming point of view, that's a terrible naming system. It would be much easier if the small one was called s_filename.jpg and the large one was called x_filename.jpg. Or even better, if you had two separate folders, large/filename.jpg, and small/filename.jpg.

  3. #3
    Join Date
    May 2009
    Posts
    5
    Ok, so maybe the whole code is more helpful

    PHP Code:
    // Define Global Vars
    var detailSlider
    var pageScroll
    var sessionCookie
    var prefetch = [];
    var 
    size Cookie.read("size");
    var 
    imageName '';
    var 
    imageSizes  = [];
    var 
    resizeTimer '';
    var 
    next =  '';
    var 
    prev ''
    var about false;

    // Verify that the size cookie is valid:
    if(size == null || size.toInt() < 0){
        var 
    size 'auto';
    }else{
        var 
    size size.toInt();
    }


    // Take the current browser size, and determine the correct 
    // image size to load. (Or load the size specified in the cookie)
    function chooseSize () {
        
        
    // If a size has already been set via a cookie,
        // load that size instead:
        
    if(size != 'auto'){
            return 
    size;
        }
        
        var 
    windowSize window.getSize();
        
    // x = width y = height
        // x px (padding)
        
    if(windowSize.1230 || windowSize.850){
            return 
    0// 0 = small
        
    }else if(windowSize.>= 1230 && windowSize.>= 900){
            return 
    1// 1 = X-Large
        
    }
    }

    // Convert the size number into the actual title
    function sizeName(imgsize){
        var 
    size_name
        
        
    switch (imgsize
        { 
           case 

              
    size_name 'x_s';
              break; 
           default : 
    // 0
              
    size_name 's'
        }
        return 
    size_name;
    }

    /*
        FUNCTION: loadImage( animate [bool,optional], manual [int,optional] )

        This function loads the appropiate image size depanding on the user's browser window.
        It can also load a specefied size if the variable "manual" is set.
        Animation is enabled by default, but can be turned off by setting animate to false.
    */
    function loadImage(animatemanual){
        
        
    // Only load the small image by default on iPods:
        
    if(Browser.Platform.ipod == true && manual === false){
            return 
    false;
        }
        
        if(
    manual !== false){
            var 
    detectSize manual.toInt();
            
    size detectSize;
            
    Cookie.write('size'size);
        }else if(
    size != 'auto'){
            var 
    detectSize size;
        }else{
            var 
    detectSize chooseSize();
            
    Cookie.write('auto_size'detectSize);
        }
        
        
        if(
    imageSizes[detectSize] == '0x0'){

            while(
    detectSize !== && imageSizes[detectSize] == '0x0'){
                
    detectSize detectSize-1;
            }
            
        }
        
        var 
    imgSize imageSizes[detectSize].split('x');
        var 
    name sizeName(detectSize);
        
        
    // currentSize = $('wrapper').getStyle('width').toInt();
        
    imgSize[0] = imgSize[0].toInt();
        
    imgSize[1] = imgSize[1].toInt();
        
        var 
    ratio imgSize[0]/imgSize[1];
        
        if(
    imgSize[0] < 800){
            var 
    pagewidth 800;
        }else{
            var 
    pagewidth imgSize[0];
        }
        
        if(
    imgSize[0] == || imgSize[1] == 0){
            return 
    false;
        }
        
        
        $$(
    '#size a').each(function(item){
            if(
    detectSize == item.rel){
                
    item.addClass('active');
            }else{
                
    item.removeClass('active');    
            }
        });
        
        if($(
    'photo').getStyle('width').toInt() == (imgSize[0] )){
            
    // No Change
            
    return false;
        }else if(
    animate == false){
            
    // Without Animation:
            
    $('wrapper').setStyle('width',(pagewidth ));
        }else{
            
    // With Wnimation:
            
    $('wrapper').tween('width', (pagewidth ));
        }
        
        var 
    img = $('image');
        
        if(
    animate == false){
            
    // Without Animation:
            
    img.setProperty('width',imgSize[0]);
            
    img.setProperty('height',imgSize[1]);
            
            if(
    ratio <= 1){
                $(
    'photo').setStyle('width',(imgSize[0]));
                $(
    'photo').setStyle('height',(imgSize[1]));
            }
            
        }else{
            
    // With Animation:
            
    img.morph({widthimgSize[0], heightimgSize[1]});
            
            if(
    ratio <= 1){
                $(
    'photo').morph({width: (imgSize[0]+26), height: (imgSize[1])});
            }
        }
        
        
        
    img.setProperty('src','images/'+name.substr(0,1) + imageName.substr(1,120));
        
        if(
    ratio 1){
            var 
    orientation 'horizontal';
        }else if(
    ratio 1){
            var 
    orientation 'vertical';
        }else{
            var 
    orientation 'square';
        }
        
        if(!
    about){
            $(
    'wrapper').setProperty('class',name+' '+orientation);
        }else{
            $(
    'wrapper').setProperty('class',name+' '+orientation+' about');
        }
        

        
        
    // If the user is using IE6:
        
    if(Browser.Engine.trident4 != null){
            
    // Manually specify the overlay height:
            
    $('navigation').setStyle('height',imgSize[1]);
        }
        
        
        
    // Image size has change, return true:
        
    return true;
    }


    window.addEvent('domready', function() {
        
        
    about    =    $('wrapper').hasClass('about');
        
    if(!
    about){
        
    next = $('next');
        
    prev = $('prev');

        
        
    // While the page loads, might as well display the next/prev nav links...
        // if(next){next.addClass('visible');}
        // if(prev){prev.addClass('visible');}
        
        // Load the correct sized image, without animation:
        
    loadImage(false,false);

        
    // Define FX:
        
    var detailSlider = new Fx.Slide('details', {
            
    link'ignore',
            
    duration800,
            
    fps30,
            
    wheelStopsfalse,
            
    transitionFx.Transitions.Expo.easeInOut
        
    });

        var 
    pageScroll = new Fx.Scroll(window, {
            
    link'ignore',
            
    duration800,
            
    fps30,
            
    wheelStopsfalse,
            
    transitionFx.Transitions.Expo.easeInOut
        
    });

        
    // Read Sesson Cookie
        
    var sessionCookie Cookie.read("detailView");

        
    // If the previous page had hidden details, 
        // hide the details for this page as well.
        
    if(sessionCookie != 'show'){
            
    // Hide Slider:
            
    detailSlider.hide();
        }else{
            
    // If the user is using IE6:
            
    if(Browser.Engine.trident4 != null){
                $(
    'details').parentNode.setStyle('height',$('details').getSize().y+50);
            }
        }


        
        
    // end if !about

        // Make the image size links clickable:
        
    $$('#size a').each(function(link){
            
    link.addEvent('click', function(){
                
    loadImage(true,link.rel);
                return 
    false;
            });
        });


    });


    window.addEvent('resize',function(){
        
        
    // Wait 600ms before resizing the photo...
        
    resizeTimer $clear(resizeTimer);
        
    resizeTimer = (function(){ loadImage(true,false); }).delay(200);

    }); 
    the script is working propper, but unfortunately my upload is creating the file s_filename.jpg and x_s_filename.jpg

    If I rename the file x_s manually to x_filename it is working, but this can't be a solution

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.10561 seconds
  • Memory Usage 3,076KB
  • Queries Executed 13 (?)
More Information
Template Usage (31):
  • (1)SHOWTHREAD
  • (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)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)showthread_list
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (27):
  • ./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
  • ./includes/functions_threadedmode.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 (72):
  • 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_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_threaded
  • showthread_threaded_construct_link
  • 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