Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从随机函数中得到一个动态链接?_Javascript_Jquery_Random_Image - Fatal编程技术网

Javascript 如何从随机函数中得到一个动态链接?

Javascript 如何从随机函数中得到一个动态链接?,javascript,jquery,random,image,Javascript,Jquery,Random,Image,我只想从这里取出动态链接,但不是所有对象: 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]="/d

我只想从这里取出动态链接,但不是所有对象:

    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[ry]+'" height="253" width="440"></embed>')
    }   
    random_imglink()
我的意思是,让smth像$random_link$dynamic link,这样我就可以把它作为一个html代码

<embed wmode="transparent" src="$random_link$" height="253" width="440"></embed>

我很难弄清楚你在问什么,但是如果你只是想从函数中获取链接,也许是作为一个返回值,以便远离文档。写一个好主意来摆脱它,然后:

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
离题:下面是函数清理了一些:

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
变化:

不要依赖分号插入,你永远无法缩小你的脚本,而且它是魔鬼的产物。 使用数组文字。 使用索引0..2而不是1..3 因此,3降低了生成索引的复杂性
我没有考虑路径的公共部分,因为我假设以后可能会添加其他没有公共部分/文档/模板/bilgiteknolojileri/的路径。如果路径总是以此开头,那么很明显,您可以通过只列出一次,然后添加更改的位来减小脚本的大小。

不清楚您在问什么。你想得到一个返回值或类似的值吗?我的意思是,现在func的工作方式是这样的,它把整个嵌入对象放在一起,但我希望它只写动态链接,这样我就可以把它放在sciptno的另一个对象中。不,我不希望链接被警告,只是为了显示链接,或者做一个动态代码link@venom:此警报仅用于显示函数返回的内容。一旦你有了函数的返回值,你可以随心所欲地使用它——输出一个嵌入标签,把它放在一个链接中,等等。请注意,你需要在你的文档中添加一个
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;