Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question string variable concatentation .. noob =(

    Hi!
    I've been searching google and this forum without much luck. I'm a noob so I apologize if this is trivial but I have a size contraint for some code that has redundant web links. I have a simple preloader but want to save some code size:

    var preloadFlag = false;
    function preloadImages() {
    if (document.images) {
    hr4_15_o = newImage("http://www.raxrepublik.com/im/hr4_15-o.jpg");
    hr4_17_o = newImage("http://www.raxrepublik.com/im/hr4_17-o.jpg");
    hr4_19_o = newImage("http://www.raxrepublik.com/im/hr4_19-o.jpg");
    hr4_21_o = newImage("http://www.raxrepublik.com/im/hr4_21-o.jpg");
    hr4_23_o = newImage("http://www.raxrepublik.com/im/hr4_23-o.jpg");
    preloadFlag = true;
    }
    }


    One way I can see is to define a string variable to have the value of the redundant code portions:

    str linkvar = "http://www.raxrepublik.com/im/"

    and then simply magically concatenate the variable with the rest of the link:


    hr4_15_o = newImage(+linkvar+"hr4_15-o.jpg");
    hr4_17_o = newImage(+linkvar+"hr4_17-o.jpg");
    hr4_19_o = newImage(+linkvar+"hr4_19-o.jpg");
    hr4_21_o = newImage(+linkvar+"hr4_21-o.jpg");
    hr4_23_o = newImage(+linkvar+"hr4_23-o.jpg");


    Problem is I don't know Javascript well enough for the syntax or details of using a function to do it. How could I do this? I have hundreds of redundant link portions and need to clean it up and make it smaller.

    Any help greatly appreciated!
    Joe

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,522
    Thanks
    3
    Thanked 508 Times in 495 Posts
    Code:
    var linkvar='http://www.raxrepublik.com/im/';
    
    hr4_15_o = new Image();
    hr4_15_o.src=linkvar+"hr4_15-o.jpg";
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • #3
    New to the CF scene
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help vmphillips!

    var linkvar='http://www.raxrepublik.com/im/';

    hr4_15_o = new Image();
    hr4_15_o.src=linkvar+"hr4_15-o.jpg";


    I've tried and it doesn't seem to work but why couldn't you do:

    var linkvar='http://www.raxrepublik.com/im/';

    hr4_15_0=new Image(linkvar+"hr4_15_o.jpg");
    hr4_17_0=new Image(linkvar+"hr4_17_o.jpg");
    etc ...


    This seems like reasonable concatenation (I think, my days of Perl and unix scripting are rusty) and I'm not sure why it too would not work and it saves characters resulting in smaller code.

  • #4
    New to the CF scene
    Join Date
    May 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry vwphillips ... i called you vmphillips

    The newimage functions could be reorg'd with the proper syntax from vwphillips but I'm now encountering href headache.

    <A HREF="http://test.test.com/directory1/file1"
    <A HREF="http://test.test.com/directory1/file2"
    <A HREF="http://test.test.com/directory1/file3"
    <A HREF="http://test.test.com/directory1/file4"
    <A HREF="http://test.test.com/directory1/file5"

    How would the syntax be for shortening the code size for these hrefs with the redundant page links? I can't get this to work ...

    var pagelink='http://test.test.com/directory1/';

    <A HREF=pagelink+"file1"
    <A HREF=pagelink+"file2"
    <A HREF=pagelink+"file3"
    <A HREF=pagelink+"file4"
    <A HREF=pagelink+"file5"

    string concatenation so far in Javascript seems weird.

    It just doesn't make sense that you can't simply set a str var and concatenate it without doing other weird stuff.

    I am missing something, I know! I smell a rewrite too ... ughh ...

    Any help greatly appreciated!

  • #5
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,522
    Thanks
    3
    Thanked 508 Times in 495 Posts
    new Image(100,200)

    parameters force the size, though I never use this

    <a href=Var+"fred.htm"

    HTML code does not accept variables thats why we have JavaScript

    could in JS

    aobj.href=variable+aobj.href;

    but aobj would need to be identified
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/


  •  

    Posting Permissions

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