Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未传递函数参数。如何将参数正确地传递到另一个JavaScript文件的函数中?_Javascript_Html - Fatal编程技术网

未传递函数参数。如何将参数正确地传递到另一个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显示“未定义”。请帮我找

我想从另一个JavaScript文件
(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
是它的值,它可能是您购物车中或正在购买的产品的数量。