简体   繁体   English

在Javascript中从一个脚本检索变量到另一个脚本

[英]Retrieve a variable from one script to another in Javascript

I have two html files, fileA.html and fileB.html, respectively associated with two javascript files fileA.js and fileB.js 我有两个html文件,fileA.html和fileB.html,分别与两个javascript文件fileA.js和fileB.js相关联

On the first html file (fileA.html), I have differents links with same className and differents value, for exemple three firsts links: 在第一个html文件(fileA.html)上,我有不同的链接,具有相同的className和不同的值,例如三个第一个链接:

<a real="external" href="fileB.html" class="links"> value 1</a>
<a real="external" href="fileB.html" class="links"> value 2</a>
<a real="external" href="fileB.html" class="links"> value 3</a>

As you can see, the "href" tag redirects to the second html file fileB.html 如您所见,“href”标记重定向到第二个html文件fileB.html

On the file fileB.html, I have a text input form which allows the user, by entering one of the values ​​present in the previous html file (for example value 2), to have access to statistics on this value ( I simplified the problem but in fact these values ​​refer to gene names ) 在文件fileB.html上,我有一个文本输入表单,允许用户通过输入前一个html文件中存在的值之一(例如值2)来访问该值的统计信息(我简化了问题,但实际上这些值是指基因名称)

I want that when the user clicks on one of the links (so one of the values), this value becomes the defaults value to the input value of the form 我希望当用户点击其中一个链接(所以其中一个值)时,此值将成为表单输入值的默认值

I have add a Event onclick on all my links, to retrieve the value of the link (userValue = .textContent) 我在我的所有链接上添加了一个事件onclick,以检索链接的值(userValue = .textContent)

On the fileB, I know that I have to change the default value by doing this: 在fileB上,我知道我必须通过这样做来更改默认值:

form = document.getElementById('formulaire');

formulaire.value = userValue;

The problem is, I don't know how to retrieve the variable on the file A (generate when we click on a link) , to the second file 问题是,我不知道如何检索文件A上的变量(当我们点击链接时生成),到第二个文件

Maybe it's not possible in javascript .. 也许这在javascript中是不可能的..

Thank's by advance 先谢谢了

You can achieve this by using localStorage, ie up on click o the link in file a write a on click function, store the clicked value to the localstorage from there, then from the second page retrieve this stored value from local storage. 你可以通过使用localStorage来实现这一点,即点击文件中的链接点击写入点击功能,将点击的值存储到localstorage,然后从第二页面从本地存储中检索这个存储的值。 You can use the set get methods of local storage as below 您可以使用本地存储的set get方法,如下所示

localStorage.setItem("myVal", "val1");
var val = localStorage.getItem("myVal");

There are a couple of ways to do this. 有几种方法可以做到这一点。 You could either attach a query or a hash to the href of your link. 您可以将查询或哈希附加到链接的href Or even go as far and use the localStorage or sessionStorage. 甚至可以使用localStorage或sessionStorage。 However, I wouldn't advice doing so, as the value is permanent for localStorage and for both it is unclear what is happening. 但是,我不建议这样做,因为localStorage的值是永久的,并且两者都不清楚发生了什么。

The solution with a hash: 带有哈希的解决方案:

<a real="external" href="fileB.html#value=1" class="links">value 1</a>
<a real="external" href="fileB.html#value=2" class="links">value 2</a>
<a real="external" href="fileB.html#value=3" class="links">value 3</a>

Then on fileB.html you could check if a hash variable is present and select that given value. 然后在fileB.html您可以检查是否存在哈希变量并选择给定值。

 const ALLOWED_FIELDS = ['value']; function selectGivenValue() { const hash = location.hash.substring(1); // This allows for multiple values to be passed hash.split('&').forEach(entity => { const [name, value] = entity.split('=', 2); // Check if field can be set if (ALLOWED_FIELDS.indexOf(name) === -1) { return; } // Set all fields with the attribute name document.querySelectorAll(`[name=${name}]`).forEach(field => { field.value = value; }); }); } // Run the function as soon as possible selectGivenValue(); 
 <select name="value"> <option value="">Please select value</option> <option value="1">Value 1</option> <option value="2">Value 2</option> <option value="3">Value 3</option> </select> <p><strong><a href="#value=1" onclick="setTimeout(selectGivenValue,100)">This link is just here to set the hash variable.</a></strong></p> 

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

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