1

我正在尝试向 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); 
}
</script>

withx是背景图片的url,可以在代码块附加到页头时动态设置。

问题是:
我得到这个:

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

在 Firebug 中报告。

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

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

4

2 回答 2

5

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

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

于 2013-02-14T15:39:50.437 回答
0

我遇到了同样的问题,它仅在您使用 sessionStorage 方法在本地打开文件时才会发生,因此请在 wamp 等 Web 服务器中运行它。

于 2015-01-14T09:49:07.987 回答