简体   繁体   English

使用javascript或html5将参数传递给url

[英]Passing a parameter to a url using javascript or html5

Newbie here.... I have an html5 and javascript webpage where a user can select an image by pausing a video. 我是新手。...我有一个html5和javascript网页,用户可以在其中暂停视频来选择图片。 I display a thumbnail of the image displaying at the pause time, and the image either side of it in case the timings are tricky. 我会显示在暂停时间显示的图像的缩略图,并在图像的两边显示图像,以防时序困难。 This works well. 这很好。 Now I have three variables that hold the name of the .jpg files selected (there are 217 of them). 现在,我有三个变量来保存所选.jpg文件的名称(其中有217个)。 I want to open a new web page and pass it the .jpg filename of the image the user has clicked to order a print. 我想打开一个新网页,并将用户单击以订购打印图像的.jpg文件名传递给它。 I have looked at many ways to pass variables to urls: query strings, sessionStorage, form with link method and others. 我研究了许多将变量传递给url的方法:查询字符串,sessionStorage,具有链接方法的表单等。 All of them tell me how to pass a text string, which works fine. 他们所有人都告诉我如何传递文本字符串,效果很好。 How do I pass a variable? 如何传递变量?

For example, the statements below works fine, but the sessionStorage returns null. 例如,以下语句可以正常工作,但是sessionStorage返回null。

var nextImg = "VideoImages/sniperImg" + i + ".jpg";
document.getElementById("threeOfThree").src = nextImg; //this works 
sessionStorage.name=nextImg; //this does not

Another example: 另一个例子:

<a href="OrderPrint.html?imgSel=preImg"
  img id="oneOfThree" src="" width="226" height="300" >
</a>

This works OK if I specify a text string like "?image.jpg", but not if I specify the variable name. 如果指定文本字符串(例如“?image.jpg”),则此方法正常,但如果指定了变量名,则无效。

I am sure the answer is obvious to others, but I would really value some help here. 我相信答案对其他人来说是显而易见的,但是在此我真的很有价值。

If you want to send a parameter to another page, then you need to set the url and query string in the href tag of an a tag. 如果要将参数发送到另一个页面,则需要在标签的href标签中设置url和查询字符串。

For example: 例如:

<a id="myatag" href="">
    <!-- Whatever you want the user to click-->
</a>

and the js: 和js:

document.getElementById("myatag").href = "OrderPrint.html?image=" + "sniperImg" + i;

Then, you can retrieve the name of the image on the server side script, and append ".jpg" or whatever extension you are using. 然后,您可以在服务器端脚本上检索图像的名称,并附加“ .jpg”或您正在使用的任何扩展名。 For example, in PHP: 例如,在PHP中:

$image = "VideoImages/" + $_GET["image"] + ".jpg";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM