刷新随机图像?

时间:2013-09-25 10:59:35

标签: javascript random

我有一个网站,每次加载主页时都有随机图像。但是当我再次点击回到主页按钮时,随机图像的问题仍然存在。确切地说,我希望随机图像仅在我刷新主页而不是主页按钮时显示,我该怎么做?

以下是代码:

JAVASCRIPT<Head>
<script type="text/javascript">
 var y1 = 20;  
(document.getElementById) ? dom = true : dom = false;


var gallery = new Array();
gallery[0] = new      Array("earlywork001.jpg","earlywork002.jpg","earlywork003.jpg","earlywork004.jpg","earlywor k005.jpg");

function pickImageFrom(whichGallery)
{
var idx = Math.floor(Math.random() * gallery[whichGallery].length);
document.write('<a href="images/earlywork/' + gallery[whichGallery][idx] + '"    class="highslide" onclick="return hs.expand(this, config1 )"><img src="images/earlywork/' +   gallery[whichGallery][idx] + '" width="402" height="180"></a>');
}

function hideIt() {
if (dom) {document.getElementById("popup").style.visibility='hidden';}
}

function showIt() {
if (dom) {document.getElementById("popup").style.visibility='visible';}
}

function placeIt() {
if (dom && !document.all) {document.getElementById("popup").style.top =    window.pageYOffset + (window.innerHeight - (window.innerHeight-y1)) + "px";}
if (document.all) {document.all["layer1"].style.top =   document.documentElement.scrollTop + (document.documentElement.clientHeight -   (document.documentElement.clientHeight-y1)) + "px";}
window.setTimeout("placeIt()", 10); }
</script>
</head>

HTML<Body>
<body onLoad="placeIt(); showIt()">
/*code*/
  <div id="content"><!--Start content-->
  <div id="popup" style="position:absolute; left:140px; width:410px; height:20px;  visibility:hidden">
   <div style="background-color:white; padding:3px; border:1px solid black">
   <span style="float:right; background-color:gray; color:white; font-weight:bold;   width='20px'; text-align:center; cursor:pointer" onclick="javascript:hideIt()">&nbsp;X&nbsp;</span>
   <script language="javascript">pickImageFrom(0);</script></div>
   </div>
   <img src="images/home.png" width="644" height="363" alt="home" class="content" />
   </div><!--End content-->
   </div><!--End body content-->
  /*code*/

2 个答案:

答案 0 :(得分:1)

前一段时间我在搜索类似的解决方案时遇到了一些麻烦,所以我写了这个,我把它贴在这里,以防其他人再次搜索这个。

window.onload = choosePic;

var images = new Array("https://randomuser.me/api/portraits/men/2.jpg", "https://randomuser.me/api/portraits/women/2.jpg", "https://randomuser.me/api/portraits/men/52.jpg", "https://randomuser.me/api/portraits/women/12.jpg", "https://randomuser.me/api/portraits/women/7.jpg", "https://randomuser.me/api/portraits/men/27.jpg", "https://randomuser.me/api/portraits/women/13.jpg", "https://randomuser.me/api/portraits/women/4.jpg", "https://randomuser.me/api/portraits/men/52.jpg", "https://randomuser.me/api/portraits/men/6.jpg", "https://randomuser.me/api/portraits/men/23.jpg", "https://randomuser.me/api/portraits/women/41.jpg", "https://randomuser.me/api/portraits/women/28.jpg");

function choosePic(){
    var randomNum = Math.floor(Math.random() * images.length);
    document.getElementById("random-image").src = images[randomNum];
}
<img src="" alt="me" id="random-image">

答案 1 :(得分:0)

将您的代码包裹在if

if (document.URL == document.referrer) {
// your code here
}

document.referrer会为您提供上一页的网址。将其与document.URL中的当前网址相匹配。如果两者都不匹配,那么您的页面就会从另一个页面导航到。