Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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中导航到另一个html页面_Javascript - Fatal编程技术网

如何在javascript中导航到另一个html页面

如何在javascript中导航到另一个html页面,javascript,Javascript,我有一个名为default.html的主html文件,其中有两个javascript文件default.js和backend.js 这两个javascript文件在主html中添加如下 <script src="assets/js/backend.js"></script> <script src="assets/js/default.js"></script> 在backend.js中,如果我调用show

我有一个名为default.html的主html文件,其中有两个javascript文件default.js和backend.js

这两个javascript文件在主html中添加如下

<script src="assets/js/backend.js"></script>
<script src="assets/js/default.js"></script>
在backend.js中,如果我调用showNextPage()在页面加载时执行,页面更改会起作用

showNextPage()

class auth {
}

class storageManager {
}
但是如果我调用showNextPage()在这样的类中执行

class auth {
     //...
 showNextPage()
}
   
仅执行showNextPage()中的console.log消息,而不执行页面更改

我不知道发生了什么,由于某种原因,它似乎是窗口。位置更改只在页面加载的早期起作用

这可能是什么原因造成的?我如何修复它

我试过了

window.location.href
window.location.replace

以及其他类似的方法。最重要的是,这个函数已经被证明是有效的,但并不是在一个让我困惑了好几天的类中。我已经多次检查我的代码,但没有结果

请尝试将您的
default.js
文件放入此文件

 startUp()
  
eel.expose(startUp);
function startUp(){
    
 showNextPage()  
    
}

function showNextPage(){

 console.log("show next page")
  window.pathname = '/next.html' //IMPORTANT PART.. origin is the page, like https://stackoverflow.com.. pathname is everything past that

 console.log("i guess the page did not change")
}

我希望它能起作用

尝试将您的
default.js
文件放入此文件

 startUp()
  
eel.expose(startUp);
function startUp(){
    
 showNextPage()  
    
}

function showNextPage(){

 console.log("show next page")
  window.pathname = '/next.html' //IMPORTANT PART.. origin is the page, like https://stackoverflow.com.. pathname is everything past that

 console.log("i guess the page did not change")
}

我希望它能工作

后端.js做的第一件事就是从
default.js
调用一个函数,这个函数可能还没有加载。您应该向
脚本
引用添加
延迟
,以便它们在运行之前等待所有内容加载。@ScottMarcus如何添加延迟?最好使用window.location.href。将“延迟”添加到脚本标记作为属性。您可能还应该侦听page ready事件。@John我尝试了该方法和许多其他方法
backend.js
做的第一件事是调用
default.js
中的函数,该函数可能尚未加载。您应该向
脚本
引用添加
延迟
,以便它们在运行之前等待所有内容加载。@ScottMarcus如何添加延迟?最好使用window.location.href。将“延迟”添加到脚本标记作为属性。您可能还应该侦听page ready事件。@John我尝试了该方法和许多其他方法