View Full Version : string variable concatentation .. noob =(

05-24-2006, 10:29 PM
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!

05-24-2006, 10:51 PM
var linkvar='http://www.raxrepublik.com/im/';

hr4_15_o = new Image();

05-25-2006, 01:32 AM
Thanks for the help vmphillips!

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

hr4_15_o = new Image();

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.

05-25-2006, 03:07 AM
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!

05-25-2006, 08:55 AM
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


but aobj would need to be identified

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum