2010-03-31 49 views
0

我想创建一个包含每个js文件的js文件,以将它们附加到它所在的head标签。将js文件附加到一个js文件,但带有JQuery错误!

但我得到这个错误

Microsoft JScript runtime error: Object expected 

这是我的代码:

var baseUrl = document.location.protocol + "//" + document.location.host + '/yabant/'; 

// To find root path with virtual directory 
function ResolveUrl(url) { 
    if (url.indexOf("~/") == 0) { 
     url = baseUrl + url.substring(2); 
    } 
    return url; 
} 

// JS dosyalarının tek noktadan yönetilmesi 
function addJavascript(jsname, pos) { 
    var th = document.getElementsByTagName(pos)[0]; 
    var s = document.createElement('script'); 
    s.setAttribute('type', 'text/javascript'); 
    s.setAttribute('src', jsname); 
    th.appendChild(s); 
} 

addJavascript(ResolveUrl('~/js/1_jquery-1.4.2.min.js'), 'head'); 
$(document).ready(function() { 
    addJavascript(ResolveUrl('~/js/5_json_parse.js'), 'head'); 
    addJavascript(ResolveUrl('~/js/3_jquery.colorbox-min.js'), 'head'); 
    addJavascript(ResolveUrl('~/js/4_AjaxErrorHandling.js'), 'head'); 
    addJavascript(ResolveUrl('~/js/6_jsSiniflar.js'), 'head'); 

    addJavascript(ResolveUrl('~/js/yabanYeni.js'), 'head'); 
    addJavascript(ResolveUrl('~/js/7_ResimBul.js'), 'head'); 
    addJavascript(ResolveUrl('~/js/8_HaberEkle.js'), 'head'); 
    addJavascript(ResolveUrl('~/js/9_etiketIslemleri.js'), 'head'); 
    addJavascript(ResolveUrl('~/js/bugun.js'), 'head'); 
    addJavascript(ResolveUrl('~/js/yaban.js'), 'head'); 
    addJavascript(ResolveUrl('~/embed/bitgravity/functions.js'), 'head'); 
}); 

路径是正确的。我想告诉你文件夹结构和面板: alt text

任何帮助将不胜感激。

回答

1

我认为问题在于当您使用$ global时,jQuery未完成加载。根据Loading Scripts Without Blocking,这种机制并不总是在IE中保留执行顺序。

简单的解决方案是使用静态脚本标签来加载jQuery。 I.E.明确地把HTML:

但你真的需要等待ready事件加载其他脚本,或者你只是需要jQuery加载?

+0

“document.write脚本标记 - 使用document.write将

0
// url=> http: + // + localhost:4399 + /yabant/ 
//  ----  ---- -------------- -------- 
//  protocol + "//" +  host  + '/virtualDirectory/' 
var baseUrl = document.location.protocol + "//" + document.location.host + '/yabant/'; 

// If there is "~/" at the begining of url, replace it with baseUrl 
function ResolveUrl(url) { 
    if (url.indexOf("~/") == 0) { 
     url = baseUrl + url.substring(2); 
    } 
    return url; 
} 

// Attaching scripts to any tag 
function addJavascript(jsname, pos) { 
    var th = document.getElementsByTagName(pos)[0]; 
    var s = document.createElement('script'); 
    s.setAttribute('type', 'text/javascript'); 
    s.setAttribute('src', jsname); 
    th.appendChild(s); 
} 

// I want to make sure jQuery is loaded? 
addJavascript(ResolveUrl('~/js/1_jquery-1.4.2.min.js'), 'head'); 

var loaded = false; // assume it didn't first and if it is change it to true 
function fControl() { 
    // alert("JQUERY is loaded?"); 
    if (typeof jQuery == 'undefined') { 
     loaded = false; 
     fTry2LoadJquery(); 
    } else { 
     loaded = true; 
     fGetOtherScripts(); 
    } 
} 

// Check is jQuery loaded 
fControl(); 

function fTry2LoadJquery() { 
    // alert("JQUERY didn't load! Trying to reload..."); 
    if (loaded == false) { 
     setTimeout("fControl()", 1000); 
    } else { 
     return; 
    } 
} 

function getJavascript(jsname, pos) { 
    // I want to retrieve every script one by one 
    $.ajaxSetup({ async: false, 
     beforeSend: function() { 
      $.ajaxSetup({ async: false }); 
     }, 
     complete: function() { 
      $.ajaxSetup({ async: false }); 
     }, 
     success: function() { 
      // 
     } 
    }); 

    $.getScript(ResolveUrl(jsname), function() { /* ok! */ }); 
} 

function fGetOtherScripts() { 
    // alert("Other js files will be load in this function"); 

    getJavascript(ResolveUrl('~/js/5_json_parse.js'), 'head'); 
    getJavascript(ResolveUrl('~/js/3_jquery.colorbox-min.js'), 'head'); 
    getJavascript(ResolveUrl('~/js/4_AjaxErrorHandling.js'), 'head'); 
    getJavascript(ResolveUrl('~/js/6_jsSiniflar.js'), 'head'); 

    getJavascript(ResolveUrl('~/js/yabanYeni.js'), 'head'); 
    getJavascript(ResolveUrl('~/js/7_ResimBul.js'), 'head'); 
    getJavascript(ResolveUrl('~/js/8_HaberEkle.js'), 'head'); 
    getJavascript(ResolveUrl('~/js/9_etiketIslemleri.js'), 'head'); 
    getJavascript(ResolveUrl('~/js/bugun.js'), 'head'); 
    getJavascript(ResolveUrl('~/js/yaban.js'), 'head'); 
    getJavascript(ResolveUrl('~/embed/bitgravity/functions.js'), 'head'); 
} 
相关问题