如何基于查询字符串在HTML中创建链接

时间:2019-05-08 16:29:42

标签: html string image hyperlink

我希望能够根据URL内的QueryString参数动态生成图像的来源。

EX:

URL = http://website.com/image.html?YYYY=2019&MM=01&DD=01

这将生成以下图像标签:

<img src="http://imageserver.com/images/2019-01-01.jpg" />

这在HTML中可行吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

尝试!

function createIMG(ulr){
//Get form.
var yearRe = /YYYY=????/;
var monthRe = /MM=??/;
var dayRe = /DD=??/;

var dayString = dayRe.exec(url.toString())[0].toString();
var monthString = monthRe.exec(url.toString()[0].toString();
var yearString = yearRe.exec(url.toString())[0].toString();
//Extract year, month, day.
var subYearString = yearString.substring(5);
var submonthString = monthString.substring(3);
var subdayString = dayString.substring(3);
var path = "http://imageserver.com/images/"+"subYearString"+"-"+"submonthString"+"-"+"subdayString".jpg";
//Create a <img>
var para = document.createElement("img");
para.setAttribute("src", "path");
//Append <img> to <div> with id="yourDIV" 
document.getElementById("yourDIV").appendChild(para);		
};
createIMG(url);