有没有一种在不刷新页面的情况下重新执行JS的方法?
如果我有父页和内页,请说。当调用内部页面时,通过ajax调用它,替换父页面的内容。当用户单击back时,我希望将它们导航回父页面,而不必重新加载页面。但是,父页面UI依赖于javascript,所以在他们单击后,我想重新执行父页面的javascript。这个是可能的吗?
编辑:这里是一些代码。我将代码包装在一个函数中,但您将在何处以及如何调用此函数?
function executeGlobJs() {
alert("js reload success");
}发布于 2016-02-12 15:22:34
您可以使用html5历史记录-api:
在click-handler中,您将调用pushState-method,stat存储当前状态以供以后重用:
$(document).on('click', 'a.link', function () {
// some ajax magic here to load the page content
// plus something that replaces the content...
// execute your custom javascript stuff that should be called again
executeGlobJs()
// replace the browser-url to the new url
// maybe a link where the user has clicked
history.pushState(data, title, url);
})如果用户浏览回:...later:
$(window).on('popstate', function () {
// the user has navigated back,
// load the content again (either via ajax or from an cache-object)
// execute your custom stuff here...
executeGlobJs()
})这是一个非常简单的例子,当然也不完美!你应该在这里读到更多关于它的内容:
对于与ajax和DOM相关的部分,您应该需要了解一下jQuery http://api.jquery.com/jquery.ajax/。(一切都是关于神奇的美元符号)
另一个选择是hashchange-event,如果您必须支持较旧的浏览器.
发布于 2016-02-12 07:52:53
您可以将所有javascript封装到一个函数中,并在页面加载时调用该函数。最终,这将使您可以在不重新加载页面的情况下重新执行整个javascript。
这是常见的做法,当你使用任何串通实用程序(例如。吞咽)
发布于 2016-02-12 08:01:56
如果您想重新加载脚本文件,就像它将在页面上重新加载一样,请查看这。
对于所有其他需要的脚本函数,只需创建一个包装函数,就像@s4n989和@所写的那样。然后,当您需要重新设置页面时,执行该函数。
https://stackoverflow.com/questions/35356640
复制相似问题