未传递函数参数。如何将参数正确地传递到另一个JavaScript文件的函数中?
我想从另一个JavaScript文件未传递函数参数。如何将参数正确地传递到另一个JavaScript文件的函数中?,javascript,html,Javascript,Html,我想从另一个JavaScript文件(page1.js)调用文件(page2.js)的函数file2Function(getValue),在浏览器中打开新页面,并使用通过myFunction1(e)传递到file2Function中的参数作为file2Function(itemFromItems)显示其中的一些数据。因此file2Function(getValue)应该取var itemFromItems的值,并且应该显示在page2.html上。但是它为getValue显示“未定义”。请帮我找
(page1.js)
调用文件(page2.js)
的函数file2Function(getValue)
,在浏览器中打开新页面,并使用通过myFunction1(e)传递到file2Function中的参数作为file2Function(itemFromItems)
显示其中的一些数据。因此file2Function(getValue)
应该取var itemFromItems
的值,并且应该显示在page2.html
上。但是它为getValue
显示“未定义”。请帮我找出我犯的错误。多谢各位
//page1.js
函数file1Function()
{
var secPage1=document.getElementById(“page1Section”);
var heading=document.createElement(“h4”);
var anchor=document.createElement('a');
setAttribute('href',“#”);
anchor.innerText=“单击我”;
anchor.addEventListener(“单击”,myFunction1);
标题.儿童(锚);
第二页1.附加子项(标题);
锚点id=1;
功能myFunction1(e){
var itemFromItems=e.currentTarget.getAttribute(“id”);
console.log(“item”,itemFromItems);
File2函数(itemFromItems);
}
}
//第2.js页
球形变种;
函数file2Function(getValue)
{
日志(“getValue”,getValue);
打开(“page2.html”);
//窗口打开(“https://www.google.com/");
globalVar=getValue;
console.log(“globalVarNow”,globalVar);
}
函数loadDynamicData()
{
var para=document.getElementById(“paraId”);
控制台日志(第2段);
para.innerText=globalVar+“:来自myFunction1的值(单击事件)”;
var secPage2=document.getElementById(“page2Section”);
var headingJS2=document.createElement(“h4”);
headingJS2.innerText=“这是动态标题”;
secPage2.附录子项(标题JS2);
控制台日志(第2段);
}
这是第一页
这是第二页
问题在于globalVar
是一个全局变量,但是您在file2Function()
中分配给它的值只能在其范围内访问,这就是为什么您得到未定义的,因为loadDynamicData()
无法访问globalVar
的新值
您可以从file2Function()
调用loaddynamicata()
,并将getValue
作为参数传递,但由于您需要在执行loaddynamicata()
之前打开page2
,因此它无法工作
我建议您将getValue
和URL
一起作为查询参数传递,然后从loaddynamicata()
内部获取,这样就可以了
解决方案:
function file2Function(getValue)
{
window.open("page2.html?value="+getValue);
// window.open("https://www.google.com/");
}
function loadDynamicData()
{
var url_string = window.location.href
var url = new URL(url_string);
var globalVar = url.searchParams.get("value");
var para=document.getElementById("paraId");
console.log(para);
para.innerText=globalVar+" : value from myFunction1 (Click Event)";
var secPage2 = document.getElementById("page2Section");
var headingJS2 = document.createElement("h4");
headingJS2.innerText=" This is dynamic heading";
secPage2.appendChild(headingJS2);
console.log(para);
}
如果要隐藏值,可以使用sessionStorage
而不是查询参数:
function file2Function(getValue)
{
sessionStorage.setItem('value', getValue)
window.open("page2.html");
// window.open("https://www.google.com/");
}
function loadDynamicData()
{
var globalVar = sessionStorage.getItem('value')
var para=document.getElementById("paraId");
console.log(para);
para.innerText=globalVar+" : value from myFunction1 (Click Event)";
var secPage2 = document.getElementById("page2Section");
var headingJS2 = document.createElement("h4");
headingJS2.innerText=" This is dynamic heading";
secPage2.appendChild(headingJS2);
console.log(para);
}
这些页面是本地的还是在同一个域中?不是。这些是本地的。感谢您在回复我的查询时付出的努力。我是否应该在file1function()之后将file2Function()放在page1.js中?是否可以在不影响其(值)在函数中的使用的情况下隐藏URL中的值或将其替换为任何伪文本?我是否应该在file1function()之后将file1function()放在page1.js中。不,你不应该,这是一个打字错误,我的意思是file2Function()
。我已经编辑过了。是否可以从URL中隐藏值…?
我看到的最好的方法是使用会话存储
,我在答案中添加了它我发现许多这样的URL在每个产品页面的末尾都包含=1
:我不确定您的具体情况,但查询是通过URL传递的参数,可以从其他页面访问。在PHP中,通过方法POST
和GET
使用它更为常见。可能containing
是一个变量,1
是它的值,它可能是您购物车中或正在购买的产品的数量。