javascript 重新加载javascript而不刷新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9289948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reload javascript without refreshing page
提问by user1210772
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.
我建立了一个使用 AJAX 的站点来加载页面内容而不必离开页面。我有几个页面使用 javascript 加载列表信息。
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.
我有一个名为 $list_id 的变量,并使用它和一些 javascript 来加载列表。我面临的问题是,如果我加载一个列表(比如 ID 为 1 的列表),然后返回并加载另一个列表(比如 ID 为 2 的列表),javascript 没有重新加载以解释新的列表 ID。
Is there anyway I can reload the javascript code to account for the new variable without having to refresh the page?
无论如何我可以重新加载javascript代码来解释新变量而无需刷新页面?
Thanks!
谢谢!
回答by Kevin
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
或使用 jQuery.loadScript 函数
回答by Lucero
When inserting HTML into the DOM scripts are executed. Therefore, when you load some HTML via AJAX you can execute contained scripts.
将 HTML 插入到 DOM 脚本中时会执行。因此,当您通过 AJAX 加载一些 HTML 时,您可以执行包含的脚本。
回答by Shea
Typically, it would be best to design a protocol here. You should have one function to parse any 'lists' being loaded, one function to parse the URL hash, and one function to load new lists. You can get the URL hash with location.hash
.
通常,最好在这里设计一个协议。您应该有一个函数来解析正在加载的任何“列表”,一个函数来解析 URL 哈希,以及一个函数来加载新列表。您可以使用location.hash
.
Here's a simple example, that expects the list to be a certain format. So it would be protocol based, as in your Javascript should only need to be loaded once, and it should expect a certain specific format that it would be capable of parsing without any added dependencies. It would be okay to actually require dependencies, but how/when/where should all be worked into the protocol.
这是一个简单的例子,它期望列表是某种格式。所以它将是基于协议的,因为在你的 Javascript 中应该只需要加载一次,并且它应该期望某种特定的格式,它能够在没有任何附加依赖项的情况下进行解析。实际上需要依赖关系是可以的,但是如何/何时/何地都应该在协议中工作。
<a class="listLoader" href="#list.php?list=1">List 1</a>
<a class="listLoader" href="#list.php?list=2">List 2</a>
<a class="listLoader" href="#list.php?list=3">List 3</a>
.
.
$('.listLoader').onclick(function() {
$.ajax({
'success': function () {
//Do what you gotta do with your lists
},
'url': location.hash.substring(1)
});
});
回答by TommyBs
Could you not store the $list_id in a hidden input field and then update it after an ajax call?
您不能将 $list_id 存储在隐藏的输入字段中,然后在 ajax 调用后更新它吗?
e.g.
例如
<input type="hidden" id="list-id" value="<?php echo $list_id; ?>" />
<script>
//ajax request
var $list_id = $('#list-id');
$.ajax({
url:'http://www.example.com/page.php',
data:'list_id=' + $list_id.val(),
success:function(data){
//do something
$list_id.val() = $list_id.val() + 1;
}
});
just cobbled this together quickly to give you an idea, so there might be some js errors
只是快速拼凑起来给你一个想法,所以可能会有一些 js 错误
回答by techfoobar
You can use jQuery's getScript()
function for getting (and executing) scripts dynamically. For example:
您可以使用 jQuery 的getScript()
函数来动态获取(和执行)脚本。例如:
$.getScript("/js/myscript.js");
回答by Julian Hollmann
As you posted no Code at all I'm taking a wild guess here.
由于您根本没有发布任何代码,因此我在这里进行了疯狂的猜测。
You don't have to reload your Javascript Code in this case but you could propably store your data in an object.
在这种情况下,您不必重新加载您的 Javascript 代码,但您可以适当地将数据存储在一个对象中。
Please show us some JS Code to answer your question properly.
请向我们展示一些 JS 代码以正确回答您的问题。