在动态插入javascript到页面时如何防止“操作不安全”警告?

我正在尝试动态地将一个元,链接和脚本块添加到jQuery Mobile页面。

该脚本包含一条规则,我通过javascript添加到CSS样式表(不幸的是必须像这样)。

看起来像这样:

<script type="text/javascript" if ('addRule' in sheet) { sheet.addRule(".splash:before", "background: url("' + x + '") no-repeat center center fixed; " + "-webkit-background-size: 100%; -moz-background-size: 100%; " + "-o-background-size: 100%; background-size: 100%; " + "-webkit-background-size: cover; -moz-background-size: cover;" + "-o-background-size: cover; background-size: cover;", 0); } else if ('insertRule' in sheet) { sheet.insertRule(".splash:before { " + "background: url("' + x + '") no-repeat center center fixed; " + "-webkit-background-size: 100%; -moz-background-size: 100%; " + "-o-background-size: 100%; background-size: 100%; " + "-webkit-background-size: cover; -moz-background-size: cover; "+ "-o-background-size: cover; background-size: cover;" + " }", 0); }  

x是背景图像url,可以在代码块附加到页面头时动态设置。

问题是:
我明白这个:

 SecurityError: The operation is insecure. [Break On This Error] slice.call( docElem.childNodes, 0 )[0].nodeType; 

在Firebug中报道。

如果我对x的URL进行硬编码,它可以正常工作,所以我假设浏览器抱怨正在使用的URL变量。

题:
知道如何规避这个吗? 我需要动态传递URL。

这几乎总是与同源政策有关的问题。 这意味着您正在加载的文件(背景图像,javascript文件,css文件等)必须是相同的域,相同的子域,相同的协议(http vs https)和相同的端口。

另外,您是在本地还是在服务器上运行? 在本地运行时,您将遇到这些问题,因为原产地在技术上是“ file:/// ”,因此如果您提供指向服务器上托管的文件的链接,您可能会收到这些错误。

我遇到了同样的问题,只有当你用sessionStorage方法在本地打开文件时才会发生这种情况,所以在像wamp等的web服务器上运行它。