Javascript 在不同域上动态加载样式表不会';我不能在Firefox中工作
我正在尝试使用引用另一个域的javascript动态加载css和js。在IE和Chrome上,一切正常,但在Firefox中不起作用,我得到以下错误:Javascript 在不同域上动态加载样式表不会';我不能在Firefox中工作,javascript,jquery,html,css,firefox,Javascript,Jquery,Html,Css,Firefox,我正在尝试使用引用另一个域的javascript动态加载css和js。在IE和Chrome上,一切正常,但在Firefox中不起作用,我得到以下错误: InvalidAccessError: A parameter or an operation is not supported by the underlying object 我已经看到很多关于这个搜索的帖子,但是我找不到任何解决方案来解决这个问题。这是我正在使用的代码 // Helper method to add js or c
InvalidAccessError: A parameter or an operation is not supported by the underlying object
我已经看到很多关于这个搜索的帖子,但是我找不到任何解决方案来解决这个问题。这是我正在使用的代码
// Helper method to add js or css files
function loadjscssfile(filename, filetype){
var fileref;
if (filetype=="js"){ //if filename is a external JavaScript file
fileref=document.createElement('script');
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", filename);
}
else if (filetype=="css"){ //if filename is an external CSS file
fileref=document.createElement("link");
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", filename);
}
if (typeof fileref!=="undefined")
document.getElementsByTagName("head")[0].appendChild(fileref);
}
function goMobile() {
// Load bootstrap and jquery 11
var script = document.createElement( 'script' );
script.onload = function () {
var bootstrap = document.createElement( 'script' );
bootstrap.onload = function() {
$jq11 = jQuery.noConflict(true);
loadjscssfile("https://example.com/inject/bootstrap-overrides.css", "css");
};
bootstrap.src = 'https://example.com/applications/bootstrap.min.js';
document.head.appendChild(bootstrap);
};
script.src = 'https://example.com/inject/jquery-1.11.2.min.js';
document.head.appendChild(script);
}
这里还有一个JSFIDLE:
我并不真正喜欢这个解决方案,但我所做的是使用ajax阅读css并将其作为样式直接附加到页面上。我仍然对如何或者是否可以使用link标记从另一个域动态加载css感兴趣。下面是我的loadCSS方法的一个示例
function loadCSS(url){
$jq11.ajax({
url: url,
type: 'GET',
dataType: 'text',
success: function (data) {
result = data;
var css = result,
head = document.head || document.getElementsByTagName('head')[0],
style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
}
});
}
请将jQuery添加到JSFIDLE。