使用Javascript不会加载到阿贾克斯的div加载、Javascript、div、到阿贾克斯

2023-09-10 19:55:05 作者:陪睡的

我有加载HTML文件的一个非常简单的AJAX脚本到一个div。 该脚本是这样的:

I have a really simple ajax script for loading HTML files into a div. The script looks like:

<head>
<script>
function fetchResults(){
/*$(document).ready(function() {*/
                $('#slideshow').cycle({
                fx: 'fade',
                pager: '#smallnav', 
                pause:   1, 
                speed: 1800,
                timeout:  3500 
            });         
        });
</script>
<script type="text/javascript">

var bustcachevar=1 (1=yes, 0=no)
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
var bustcacheparameter=""

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
} 
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
if (bustcachevar) //if bust caching of external page
bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
page_request.open('GET', url+bustcacheparameter, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}

function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " 
}
}
}

</script>
</head>

它加载说的HTML文件整齐地进入这个DIV:

Which loads said HTML file neatly into this div:

 <div id="rss">
<script type="text/javascript">
            ajaxpage('news.html', 'content');   
        </script>
</div>

不过,对这些HTML文件中的一个,我的JavaScript是不会在分区加载一点点,而是直接查看页面正常时,负载

But on one of these HTML files, I have a tiny bit of javascript that won't load in the div, but loads fine when viewing the page directly.

<script src="http://feeds.feedburner.com/xxx/QFMN?format=sigpro" type="text/javascript" ></script>

我知道有一个问题与JavaScript的阿贾克斯,也知道有一个解决方法。 作为一个菜鸟的JavaScript,我不完全知道如何来解决这个。 有什么建议?

I know there's an issue with javascript in ajax, but also know there's a work-around. Being a noob to javascript, I'm not completely sure how to work this. Any suggestions?

推荐答案

正如你似乎已经被使用jQuery,请,请,请使用jQuery的Ajax方法做异步请求。您的所有code可以换成类似于

As you're already seem to be using jQuery, please, please, please use jQuery's Ajax methods to do asynchronous requests. All your code can be replaced with something like

$(function () {
    $('#container').html($.get('/some/other/url.html'));
});

下面有一个链接的例子,点击它取代的内容的 DIV 用AJAX调用的结果是:

Here's an example of a link, clicking on which replaces the contents of a div with the result of an AJAX call:

<a class="ajaxLink" href="/some/other/url.html">next</a>
<div id="container">
    Hello there
</div>

<script type="text/javascript">
    $(function () {
        $("a.ajaxLink").click(function () {
            $('#container').load($(this).attr('href'));
            return false;
        });
    });
</script>

请注意我们是如何巧妙地使用的href 指定URL的链接的属性 - 如果客户端没有启用JavaScript的链接只会照常上班

Note how we cleverly using the href attribute of the link to specify the URL - if the client does not have JavaScript enabled the link will just work as usual.

jQuery的文档的AJAX方法是一个有价值的信息来源。请参阅加载页面片段和执行脚本的.load()方法为文档说明在加载网页的脚本是如何执行的。

JQuery documentation on AJAX methods is a valuable source of information. See "loading page fragments" and "script execution" in the documentation of the .load() method for explanation how scripts in the loaded pages are executed.

(还,使用 JSLint的或类似的服务是非常有帮助)

(also, using jslint or a similar service is immensely helpful)