简体   繁体   English

如何将值从Java脚本传递到HTML?

[英]How to pass values from java script to HTML?

I have two files like 我有两个文件

DataProcesser.js DataProcesser.js

var FROM;
 var TO;
var CONTECT;

function getValues(fromAdd,toAdd,ContectText){
 alert("method called");
  FROM=fromAdd;
  TO=toAdd;
  CONTECT=ContectText;
 alert("values are " + FROM +" "+ TO +" "+ CONTECT);
 window.open('email.html','newwindow');
}

EMail.html EMail.html

During body onload 身体负荷期间

function loadData(){
    document.getElementById('lblFrom').innerHTML =window.FROM;
    document.getElementById('lbTo').innerHTML = window.TO;
    document.getElementById('lblfrom1').innerHTML = window.FROM;
    document.getElementById('lblto1').innerHTML = window.TO;
    document.getElementById('txtAreaBody').value =window.CONTECT;

}

How can i pass From, to, contect to EMail.html? 如何将发件人,收件人,收件人联系到EMail.html? I have written loadData function in Body Onload. 我已经在Body Onload中编写了loadData函数。 Getting undefined values as a result.Calling the getvalues function from another HTML. 结果是获取未定义的值。从另一个HTML调用getvalues函数。

Every HTML document has its own Javascript context. 每个HTML文档都有自己的Javascript上下文。 So you can't access variables from one HTML page in another. 因此,您无法从另一个HTML页面访问变量。

But what you can do, is transfer these variables as part of the URL. 但是您可以做的是将这些变量作为URL的一部分进行传输。 You can use the ?-operator to separate URL from data passed to it. 您可以使用?运算符将URL与传递给它的数据分开。 Example: 例:

email.html?from=somewhere&to=somewhereElse&connect=somevalue

The webserver will only interprete everyting up to the ? 网络服务器只会解释所有内容? as part of the filename, the rest are GET-parameters passed to it. 作为文件名的一部分,其余是传递给它的GET参数。 So the webserver will return the file email.html. 因此,网络服务器将返回文件email.html。 On that page, you can then access the complete URL and parse the arguments using the variable document.location . 然后,在该页面上,您可以访问完整的URL并使用变量document.location解析参数。

Another option is using cookies . 另一种选择是使用cookie But keep in mind that some users have cookies disabled (moreso than those which disable javascript), so you shouldn't rely on them for any primary functionality of your website. 但是请记住,某些用户禁用了cookie(比禁用javascript的cookie还要多),因此您不应依赖它们来获取网站的任何主要功能。

The best way, however, would be to store all data on the server and use a session handling mechanism which uses cookies when available and GET-parameters as a fallback to assign a session-ID to each individual user. 但是,最好的方法是将所有数据存储在服务器上,并使用会话处理机制,该机制在可用时使用Cookie,并使用GET参数作为后备,以将会话ID分配给每个用户。 The most important advantage of this is that the user can not tamper with the data that easily. 这样做最重要的优点是用户无法轻易篡改数据。 This, however, can not be done with client-sided javascript alone, because it needs server-sided scripting. 但是,这不能仅使用客户端javascript来完成,因为它需要服务器端脚本。

So the variable FROM, TO , CONTECT are being set sometime , inside getValues(). 因此,可以在getValues()中的某个时间设置变量FROM,TO,CONTECT。 And then you call loadData(), so that the HTML loaded uses the values you obtained. 然后调用loadData(),以便加载的HTML使用获得的值。 So I'm assuming getValues() and loadData() are in the same JS context and the variables are directly accessible. 因此,我假设getValues()和loadData()在同一JS上下文中,并且可以直接访问变量。

Change this code: 更改此代码:

     document.getElementById('lblFrom').innerHTML =window.FROM;

to

      document.getElementById('lblFrom').innerHTML = FROM;

Do similarly for other variables. 对其他变量进行类似的处理。

PS: When posting code, please also specify the flow. PS:发布代码时,请同时指定流程。 Posting random code snippets won't help us understand the problem. 发布随机代码段不会帮助我们理解问题。

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

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