使用 window.location.assign 加载页面后调用 javascript 函数 [英] Calling a javascript function after loading a page with window.location.assign

查看:54
本文介绍了使用 window.location.assign 加载页面后调用 javascript 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在使用 window.location.assign 加载页面后调用 javascript 函数?我有这个代码.

Is it possible to call a javascript function after loading a page with window.location.assign? I have this code.

<script language="javascript" type="text/javascript">
function Loadspirit(){
  window.location.assign("spirituality.php");
  ReadEnglish();
}
</script>

我加载页面然后调用 ReadEnglish(),这是一个在 spiritity.php 中定义的函数.该函数不执行.使函数执行的方法是什么?

I load the page then call ReadEnglish(), a function defined in spirituality.php. The function doesn't execute. What would be the way to make the function execute?

推荐答案

只要您分配一个新的 window.location,浏览器就会开始在当前浏览器窗口中加载一个新页面.这将停止执行此窗口中的所有 javascript 并开始加载新页面.从那时起,只有新页面中的 javascript 才会运行.如果在加载新页面后想要一个函数,则必须将该代码放入新页面中.如果希望该函数一直在新页面中运行,您只需将新页面中的代码设置为在页面加载时始终运行该函数.

As soon as you assign a new window.location, the browser starts loading a new page in the current browser window. That stops execution of all javascript in this window and starts loading the new page. Only javascript in the new page will run from that point on. If you want a function after that new page is loaded, you will have to put that code in the new page. If want that function to run in the new page all the time, you can just set the code in that new page to always run that function when the page is loaded.

如果您只希望新函数在从特定上下文调用时在新页面中运行,那么您需要将一些信息传递给新页面,并且新页面中的代码需要查找该信息来决定是否运行 ReadEnglish() 函数.向新页面传递信息的典型方式是

If you only want that new function to run in the new page when it's called from this particular context, then you need to pass some information to the new page and the code in the new page needs to look for that information to decide whether to run the ReadEnglish() function or not. The typical ways that you can pass information to the new page are

  1. URL 中的查询参数(URL 中 ? 标记后的项目,如?run=English").
  2. cookie 中设置的信息
  3. 新页面已知的其他窗口/框架中的 Javascript 变量并且来自同一来源.

如果这不是持久设置,而是在特定时间偶尔执行此选项,则通常使用查询参数.

Query parameters are often used if this is not a persistent setting, but rather an occasional execution of this option this particular time.

如果这是一个更持久的设置,通常会使用 Cookie,因为 cookie 可以持续存在,直到您告诉它消失为止.

Cookies are often used if this is a more persistent setting because the cookie can persist until you tell it to go away.

这篇关于使用 window.location.assign 加载页面后调用 javascript 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆