javascript - 如何在调用 window.location.href 后运行函数?
问题描述
我正在尝试重定向到另一个站点,然后在其上运行代码。
function gotonewpage() {
window.location.href = "WEBSITE"
x = document.getElementById("s")
}
解决方案
好吧,window.location.href 重新加载了应用程序,其余代码永远没有机会执行它。
这就像在函数中的 return 关键字之后使用语句。
function sum(a, b) {
var sum;
return sum;
sum = a + b; // this will never execute
}
但是你能做什么,重定向发生后,你可以添加这个
x = document.getElementById("s")
在发生重定向的文件中。但是恐怕您也必须在新文件中添加带有 id 's' 的元素。
推荐阅读
- gnat - 'With-in' 重复的 GPRBuild 名称文件
- hyperledger-fabric-sdk-go - Fabric-sdk-go client.Query 返回错误:创建事务失败:Channel_Cfg_Cache - 缓存已关闭
- reactjs - 在 react-google-login 上使用 uxMode="redirect" 时获取 access_token
- java - java.lang.ClassNotFoundException: org.slf4j.impl.StaticLoggerBinder 我什至没有登录我的依赖项
- linux - 从外部目录调用 makepkg
- visual-studio - 使用 Visual Studio 包在 nuget 包中包含构建目录
- javascript - 文件不是函数吗?
- html - 块列表和内联块显示不正确的垂直间距
- java - 无法在 Eclipse 中进行控制台配置休眠
- javascript - Google 广告已使用链接预加载进行预加载,但在窗口加载事件后的几秒钟内未使用