重新加载JavaScript而不刷新页面 [英] Reload javascript without refreshing page

查看:862
本文介绍了重新加载JavaScript而不刷新页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

免责声明:我对这些东西非常陌生,因此请记住这一点.

你好

我使用AJAX设置了一个站点,无需加载页面即可加载页面内容.我有几页使用javascript加载列表信息.

这是链接设置的外观:

#home
#list.php?list=$list_id

我有一个名为$ list_id的变量,并将其与一些javascript一起使用以加载列表.我面临的问题是,如果我加载一个列表(比如说ID为1的列表),然后返回并加载另一个列表(比如说ID为2的列表),则说明javascript无法重新加载以说明新的列表ID.

无论如何,我是否可以重新加载javascript代码以解决新变量的问题,而无需刷新页面?

谢谢!

解决方案

我不明白您到底要面对什么问题. 但是如果您想重新加载脚本而不刷新页面,则可以通过以下方式进行:

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "somefile.js";
document.getElementsByTagName("head")[0].appendChild(script);

或使用jQuery.loadScript函数

Disclaimer: I am very new to this stuff so please bare this in mind.

Hello,

Im setting up a site using AJAX to load page content without having to leave the page. I have a few pages that use javascript to load list information.

Here is link setup looks:

#home
#list.php?list=$list_id

I have a variable called $list_id and use that with some javascript to load the list. The problem I am facing is that if I load one list (lets say list with ID 1) then go back and load another list (lets say list with ID 2) the javascript has not reloaded to account for the new list ID.

Is there anyway I can reload the javascript code to account for the new variable without having to refresh the page?

Thanks!

解决方案

I dont understand what problem you are facing exactly. but if you want to reload script without refreshing the page, you can do in this way:

var script = document.createElement("script");
script.type = "text/javascript";
script.src = "somefile.js";
document.getElementsByTagName("head")[0].appendChild(script);

or use jQuery.loadScript function

这篇关于重新加载JavaScript而不刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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