Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.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中访问window.location之后的另一个函数的值_Javascript - Fatal编程技术网

在JavaScript中访问window.location之后的另一个函数的值

在JavaScript中访问window.location之后的另一个函数的值,javascript,Javascript,这是我的content.js文件 content函数由recomment.html调用,在重定向到movie.html页面后,setcontent函数将由movie.html调用 这是我的电影 但一旦浏览器重定向到movie.html,它就会在id name的位置显示undefined,而不是title值。我不确定出了什么问题。有人能帮我吗?重定向时,窗口对象将属于新窗口。以前窗口的变量在新窗口中不可用 您可以通过以下方式实现您的用例: var title function content(e)

这是我的content.js文件

content函数由recomment.html调用,在重定向到movie.html页面后,setcontent函数将由movie.html调用

这是我的电影


但一旦浏览器重定向到movie.html,它就会在id name的位置显示undefined,而不是title值。我不确定出了什么问题。有人能帮我吗?

重定向时,窗口对象将属于新窗口。以前窗口的变量在新窗口中不可用

您可以通过以下方式实现您的用例:

var title
function content(e) {
    title = e.getAttribute('title');
    // alert(title); //it alerts the title
    window.document.location=`movie?name=${title}`;
}

function setcontent(){
    document.getElementById("name").innerHTML = window.location.search.split("=")[1];;
}

元素上下文中的JavaScript程序在网页中运行

如果你浏览到一个新网页,那么你实际上是在退出该程序并运行一个新的程序

变量的寿命不会比它们所属的程序长

如果希望一个页面中的数据在另一个页面中可用,那么第一个页面需要将其放在第二个页面可以访问的地方

这些地方包括:

网址 本地存储 饼干
如果要在页面加载之间存储数据,则需要将其存储在浏览器localStorage或sessionStorage中。像这样:

function content(e) {
  localStorage.setItem('title', e.getAttribute('title'));
  window.document.location="movie";
}

function setcontent(){
    document.getElementById("name").innerHTML = localStorage.getItem('title');
}

要在页面更改中保存数据,您需要通过查询字符串传递数据,或者使用localStorage,我建议这样做。
var title
function content(e) {
    title = e.getAttribute('title');
    // alert(title); //it alerts the title
    window.document.location=`movie?name=${title}`;
}

function setcontent(){
    document.getElementById("name").innerHTML = window.location.search.split("=")[1];;
}
function content(e) {
  localStorage.setItem('title', e.getAttribute('title'));
  window.document.location="movie";
}

function setcontent(){
    document.getElementById("name").innerHTML = localStorage.getItem('title');
}