i want to take out just the dynamic link, but not all object from here:
function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="/documents/templates/bilgiteknolojileri/standalone.swf"
myimages[2]="/documents/templates/bilgiteknolojileri/mobil.swf"
myimages[3]="/documents/templates/bilgiteknolojileri/3b2.swf"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0)
ry=1
document.write('<embed wmode="transparent" src="'+myimages开发者_如何学JAVA[ry]+'" height="253" width="440"></embed>')
}
random_imglink()
i mean, to make smth like $random_link$ dynamic link so that i can put it in html code as
<embed wmode="transparent" src="$random_link$" height="253" width="440"></embed>
function randomItem(theArray) {
return theArray[Math.floor(theArray.length * Math.random())];
}
myImages = ["some","image","paths"];
var theFlashElement = '<embed wmode="transparent" src="' + randomItem(myImages) + '" height="253" width="440"></embed>';
document.getElementById("flashContainerId").innerHTML = theFlashElement;
I'm having trouble figuring out what you're asking, but if you're looking to get just the link from the function (perhaps as a return value) in order to get away from document.write
(almost always a good idea to get away from that), then:
function random_imglink(){
var myimages=new Array()
//specify random images below. You can have as many as you wish
myimages[1]="/documents/templates/bilgiteknolojileri/standalone.swf"
myimages[2]="/documents/templates/bilgiteknolojileri/mobil.swf"
myimages[3]="/documents/templates/bilgiteknolojileri/3b2.swf"
var ry=Math.floor(Math.random()*myimages.length)
if (ry==0) {
ry=1;
}
return myimages[ry];
}
alert(random_imglink()); // alerts one of the three paths above
Off-topic: Here's that function cleaned up some:
function random_imglink(){
//specify random images below. You can have as many as you wish
var myimages = [
"/documents/templates/bilgiteknolojileri/standalone.swf",
"/documents/templates/bilgiteknolojileri/mobil.swf",
"/documents/templates/bilgiteknolojileri/3b2.swf"
];
return myimages[Math.floor(Math.random()*myimages.length)];
}
alert(random_imglink()); // alerts one of the three paths above
Changes:
- Don't rely on semicolon insertion, you'll never be able to minify your scripts (and it's the spawn of the devil anyway).
- Use array literal.
- Use indexes 0..2 rather than 1..3
- As a result of #3 reduce the complexity of generating the index
I haven't factored out the common part of the paths on the assumption that there may be others added later that don't have that common part (/documents/templates/bilgiteknolojileri/
). If the paths will always start with that, then obviously you could reduce the size of the script by only listing it once and then appending the bit that changes.
精彩评论