将Javascript变量传递到URL
我有一堆URL,可以在我正在开发的网页上自动加载图像:将Javascript变量传递到URL,javascript,html,url,Javascript,Html,Url,我有一堆URL,可以在我正在开发的网页上自动加载图像: <a href="https://www.sample1.com" target="_blank"> <img src="https://www.sample1.com" alt="sample1" <a href="https://www.sample2.com" target="_blank"
<a href="https://www.sample1.com" target="_blank">
<img src="https://www.sample1.com" alt="sample1"
<a href="https://www.sample2.com" target="_blank">
<img src="https://www.sample2.com" alt="sample2">
...
...
但是,我需要显示的一些图像是动态的,并且基于当前时间。我知道如何获取UTC中的正确时间、小时和小时,然后正确定义变量:
<script type="text/javascript">
var datee = new Date()
var yyyy = datee.getFullYear()
var mm = datee.getMonth() + 1
var dd = datee.getDate()
var mms = datee.getMinutes()
var hh_UTC = datee.getUTCHours()
var outlook_hour = []
if (parseInt(hh_UTC) > 0 && parseInt(hh_UTC) < 12){outlook_hour = "0100"}
if (parseInt(hh_UTC) > 11 && parseInt(hh_UTC) < 13){outlook_hour = "1200"}
if (parseInt(hh_UTC) > 13 && parseInt(hh_UTC) < 17){outlook_hour = "1630"}
if (parseInt(hh_UTC) > 16 && parseInt(hh_UTC) < 21){outlook_hour = "2000"}
var url1 = "https://sample_img_" + outlook_hour + ".gif"
</script>
var datee=新日期()
var yyyy=datee.getFullYear()
var mm=datee.getMonth()+1
var dd=datee.getDate()
var mms=datee.getMinutes()
var hh_UTC=datee.getUTCHours()
var outlook_hour=[]
如果(parseInt(hh_UTC)>0&&parseInt(hh_UTC)<12{outlook_hour=“0100”}
如果(parseInt(hh_UTC)>11&&parseInt(hh_UTC)<13{outlook_hour=“1200”}
如果(parseInt(hh_UTC)>13&&parseInt(hh_UTC)<17{outlook_hour=“1630”}
如果(parseInt(hh_UTC)>16和&parseInt(hh_UTC)<21){outlook_hour=“2000”}
变量url1=”https://sample_img_“+outlook\u hour+”.gif”
如何提取“url1”变量,以类似于提供的第一块代码的方式在页面上加载图像?在线搜索时,我只能找到“document.getElementbyID”,其中我没有真正的“ID”。因此,大致如下:
<a href="https://www.updated_img.com" target="_blank">
<img src=document.getElementbyID(url1) alt="sample_img">
这是行不通的。因此,如何在上述代码段中传递“url1”变量才能正常工作?您需要为图像提供一个id,然后将其
src
属性设置为变量的值:
<script type="text/javascript">
var datee = new Date()
var yyyy = datee.getFullYear()
var mm = datee.getMonth() + 1
var dd = datee.getDate()
var mms = datee.getMinutes()
var hh_UTC = datee.getUTCHours()
var outlook_hour = []
if (parseInt(hh_UTC) > 0 && parseInt(hh_UTC) < 12){outlook_hour = "0100"}
if (parseInt(hh_UTC) > 11 && parseInt(hh_UTC) < 13){outlook_hour = "1200"}
if (parseInt(hh_UTC) > 13 && parseInt(hh_UTC) < 17){outlook_hour = "1630"}
if (parseInt(hh_UTC) > 16 && parseInt(hh_UTC) < 21){outlook_hour = "2000"}
var url1 = "https://sample_img_" + outlook_hour + ".gif"
</script>
HTML: