Javascript没有在AJAX,304 Firefox中加载

时间:2011-10-27 21:02:19

标签: javascript ajax

我正在加载一个下拉菜单,假设它下面加载内容。我把我的文件分解了。

  • Head =>调用Javascript / AJAX
  • Header.php =>包含与AJAX和DIV交互的下拉菜单
  • Content.php =>包含在下拉选择后显示内容的DIV

问题: 在其中一个内容框中,我遇到了来自twitter和disqus小部件的Javascript问题。如果没有在AJAX中实现,一切正常。在AJAX中,所有内容都显示为空白,Firefox正在给我304 Not Modified。


AJAX代码

// AJAX Page Reload
var bustcachevar = 1 //bust potential caching of external pages after initial request? (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, false)
    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) { //Check to see if this object has not already been added to page before proceeding
            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 + " " //Remember this object as being already added to page
        }
    }
}

/***Combo Menu Load Ajax snippet**/

function ajaxcombo(selectobjID, loadarea) {
    var selectobj = document.getElementById ? document.getElementById(selectobjID) : ""
    if (selectobj != "" && selectobj.options[selectobj.selectedIndex].value != "") ajaxpage(selectobj.options[selectobj.selectedIndex].value, loadarea)
}

位于头部的JavaScript代码

<script src="js/scripts.js" type="text/javascript"></script>
<script src="http://widgets.twimg.com/j/2/widget.js"></script>
<script type="text/javascript"> var disqus_developer = 1; </script>
<script src="//platform.twitter.com/widgets.js" type="text/javascript"></script>

下拉菜单代码位于Header.php

<form>
<select id="ajaxmenu" size="1" onChange="ajaxcombo('ajaxmenu', 'contentarea')">
<option value="issue/test1.php">test1</option>
<option value="issue/test2.php">test2</option>
<option value="issue/test3.php">test3</option>
</select>
</form>

Twitter小部件代码位于1.php

<script>
new TWTR.Widget({
  version: 2,
  type: 'search',
  search: '#Discuss',
  interval: 30000,
  title: '',
  subject: '',
  width: 255,
  height: 300,
  theme: {
    shell: {
         background: 'transparent', //this is important
    },
    tweets: {
     background: 'transparent', //this is important
      color: '#444444',
      links: '#1985b5'
    }
  },
  features: {
    scrollbar: false,
    loop: true,
    live: true,
    behavior: 'all'
  }
}).render().start();
</script>

次要问题: 当我从下拉菜单中选择一个部分并刷新页面时,下拉菜单会中断。我的意思是如果我在test1并选择test2,在加载新内容后,我刷新页面,下拉菜单将保留在test2上,但内容将在test1上。如果有人知道一个快速解决这个问题的方法,那就太棒了。

这是我第一次在这里发帖,所以如果我碰巧搞砸了,请告诉我。我试图提供尽可能多的信息,因为我一直在搜索关于如何解决这个问题的两天。

先谢谢。

0 个答案:

没有答案