Click to See Complete Forum and Search --> : help with link from array
Hello all
I have part of some code here I am working on, and I am asking for some help!
This link is an external link to another web site(s)
Link currently shows up in browser like:
http://www.mysite.com/www.othersite.com/
So after many hours I finally got the link to show up (because I am not familiar with array) now it doesn’t work properly.
document.write ("<a href=");
document.write (other_artists[i].url);
document.write (">");
document.write (other_artists[i].name);
document.write ('<a/>');
Soon after I get this to work I would like change this to link an image to the site in the array
Any help would be great!
document.write ("<a href=");
if(other_artists[i].url.substr(0,7) != "http://"){
other_artists[i].url = "http://"+other_artists[i].url;
}
document.write (other_artists[i].url);
document.write (">");
document.write (other_artists[i].name);
document.write ('</a>');
chemical
10-28-2004, 04:19 PM
deleted
Originally posted by Jona
document.write ("<a href=");
if(other_artists[i].url.substr(0,7) != "http://"){
other_artists[i].url = "http://"+other_artists[i].url;
}
document.write (other_artists[i].url);
document.write (">");
document.write (other_artists[i].name);
document.write ('</a>');
Works thanks!
so if it allready has http:// it wont add another??
Originally posted by mdb
so if it allready has http:// it wont add another??
Yes. The script checks the first seven characters in the string given. If the first seven characters are "http://," it will act normally, completely ignoring the inner statement of the if. However, if the first seven characters are not "http://," the script will add it to the beginning of the string.
I got it
its hl not h1
So it worked for other_artists right!
I copied the code cahage other to h1_artists
now getting error
'h1_artists' is undefined
if i do
document.write (h1_artists[i].url);
and
document.write (h1_artists[i].name);
both work
the code
document.write ("<a href=");
if(h1_artists[i].url.substr(0,7) != "http://"){
h1_artists[i].url = "http://"+h1_artists[i].url;
}
document.write (h1_artists[i].url);
document.write (">");
document.write (h1_artists[i].name);
document.write ('</a>');
}
document.writeln ("</b></font><font class='normal'><b>");
firstpass = true;
document.writeln (" / ");
for (i=0;i<other_artists.length;i++) {
if (firstpass)
firstpass = false;
else
document.write(", ");
document.write ("<a href=");
if(other_artists[i].url.substr(0,7) != "http://"){
other_artists[i].url = "http://"+other_artists[i].url;
}
document.write (other_artists[i].url);
document.write (">");
document.write (other_artists[i].name);
document.write ('</a>');I got it its hl not h1
Originally posted by mdb
So it worked for other_artists right!
I copied the code cahage other to h1_artists
now getting error
'h1_artists' is undefined
if i do
document.write (h1_artists[i].url);
and
document.write (h1_artists[i].name);
both work
If they are defined, the script should work. Can you post more code, so I can replicate the problem? Also, please use the CODE UBB tags so I can read it more easily.
Edit: I saw your edit. Glad you got it figured out.
So now I noticed that if there is no url it will still link the artist name (http://)
Sorry I knew this was the case but didn't know it would make a link to nothing!
This is the code the script is calling or pointing to
function artistObj (id,name,url) { this.id = id; this.name = name; this.url = url; }
hl_artists = new Array ( new artistObj('26840','Josh Groban','www.joshgroban.com'));
or it can be
hl_artists = new Array ( new artistObj('1111','some name',''));
document.write ("<a href=");
if(hl_artists[i].url.substr(0,7) != "http://"){
hl_artists[i].url = "http://"+hl_artists[i].url;
}
document.write (hl_artists[i].url);
document.write (">");
document.write (hl_artists[i].name);
document.write ('</a>');
Then just check to make sure the string isn't empty. How hard is that?
document.write ("<a href=");
if(hl_artists[i].url.substr(0,7) != "http://" && hl_artists[i].url != ""){
hl_artists[i].url = "http://"+hl_artists[i].url;
}
document.write (hl_artists[i].url);
document.write (">");
document.write (hl_artists[i].name);
document.write ('</a>');
Jona thanks for all the help, I don't know js I am going through my books and cant find anything like what I am working on.
So the last code you wrote still creates a link if no link is avail
It links to the current directory.
document.write ("<a href=");
if(hl_artists[i].url.substr(0,7) != "http://" && hl_artists[i].url != ""){
hl_artists[i].url = "http://"+hl_artists[i].url;
}
document.write (hl_artists[i].url);
document.write (">");
document.write (hl_artists[i].name);
document.write ('</a>');
}
I took a better look at the code.
Do the first lines of code look to see if there is an artist to display or not
if this is the case
how do I do this for the url?
firstpass = true;
document.writeln (" / ");
for (i=0;i<other_artists.length;i++) {
if (firstpass)
firstpass = false;
else
document.write(", ");
document.write ("<a href=");
if(other_artists[i].url.substr(0,7) != "http://"){
other_artists[i].url = "http://"+other_artists[i].url;
}
document.write (other_artists[i].url);
document.write (">");
document.write (other_artists[i].name);
document.write ('</a>');
}
Originally posted by mdb
I took a better look at the code.
Do the first lines of code look to see if there is an artist to display or not
if this is the case
how do I do this for the url?
It doesn't check anything. I don't know what you have the "firstpass" variable for, since all you're doing is setting it to fales the first time the loop runs... Anyway, the code I gave does work as expected, but you didn't expect for it to output a link at all, which is not the case. Try the following.
document.writeln (" / ");
for (i=0;i<other_artists.length;i++) {
document.write(", ");
if(other_artists[i].url != ""){
document.write ("<a href=");
if(other_artists[i].url.substr(0,7) != "http://"){
other_artists[i].url = "http://"+other_artists[i].url;
}
document.write (other_artists[i].url);
document.write (">");
document.write (other_artists[i].name);
document.write ('</a>');
} else {
document.write (other_artists[i].name);
}
}