javascript Galleria 主题偶尔加载不出来

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6181344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:47:37  来源:igfitidea点击:

Galleria theme occasionally not loading

javascriptreloadgalleria

提问by odle

I am using the Galleria slideshow on my site, but I've noticed an error that seems to happen very randomly. Most of the time the slideshow loads correctrly but once in a while I get this error:

我在我的网站上使用 Galleria 幻灯片,但我注意到一个错误似乎非常随机发生。大多数情况下,幻灯片加载正确,但偶尔我会收到此错误:

 Uncaught Error: Fatal error: Theme at javascript/themes/classic/galleria.classic.js
 could not load, check theme path.

When I reload the page, it's all back to normal. This is the code I'm using to load it:

当我重新加载页面时,一切都恢复正常。这是我用来加载它的代码:

  <script> 
        // Load the classic theme
   Galleria.loadTheme('javascript/themes/classic/galleria.classic.js');
    </script> 

I have searched around but still haven't found a solution that works. My personal idea was to have a script that keeps loading until it succeeds, since on reload the page works. How would I do this?

我四处搜索,但仍然没有找到有效的解决方案。我个人的想法是让脚本一直加载直到成功,因为在重新加载页面时可以正常工作。我该怎么做?

回答by David Hellsing

1 Try the latest build at gihub: https://github.com/aino/galleria/blob/master/src/galleria.js

1 在 gihub 尝试最新版本:https: //github.com/aino/galleria/blob/master/src/galleria.js

2 Try loading the theme using a script tag instead:

2 尝试使用脚本标签加载主题:

<script src="javascript/themes/classic/galleria.classic.js"></script>

回答by Ivan Chaer

I adopted the method pointed out by David, loading the theme using a script tag:

我采用了大卫指出的方法,使用脚本标签加载主题:

<script src="javascript/themes/classic/galleria.classic.js"></script>

But was eventually getting another error (Fatal error: Theme CSS could not load after 20 sec). I'd also recommend adding the CSS using a link tag:

但最终又出现了另一个错误(致命错误:主题 CSS 无法在 20 秒后加载)。我还建议使用链接标签添加 CSS:

<link rel="stylesheet" type="text/css" href="galleria/themes/classic/galleria.classic.css" />

回答by Benny Jobigan

I had a similar message today when I tried using Galleria. It happened only in Firefox. What I did to get around it is to add the theme stylesheet link directly in the head. I kept the theme script link in too, after the stylesheet, just in case it was needed. After that, the error message went away and Galleria is working as it should.

今天我尝试使用 Galleria 时收到了类似的消息。它只发生在 Firefox 中。我为解决这个问题所做的是直接在head. 我也保留了主题脚本链接,在样式表之后,以防万一。之后,错误消息消失了,Galleria 正常工作。

回答by jfklein

Judging from where the error message comes from, and considering the random occurrences, this issue could be from simply hitting the timeout when loading:

从错误消息的来源来看,并考虑到随机发生,这个问题可能是加载时简单地达到了超时:

Galleria.loadTheme = function( src, options ) {

var loaded = false,
    length = _galleries.length,
    err = window.setTimeout( function() {
        Galleria.raise( "Theme at " + src + " could not load, check theme path.", true );
    }, 5000 );

In version 1.2.2, the timeout is just 2 seconds, in the above (1.2.6) the timeout is 5 seconds. So upgrading to the later version, or customizing the timeout is definitely something to try.

在 1.2.2 版本中,超时时间仅为 2 秒,在上面的 (1.2.6) 中,超时时间为 5 秒。所以升级到更高版本,或者自定义超时绝对是值得一试的。

回答by Mel

Given the random behavior, it feels like a browser bug. More specifically, that the browser looses track of the base URL. I would give the full path from the webroot and see if the error goes away. For example:

鉴于随机行为,感觉就像是浏览器错误。更具体地说,浏览器失去了对基本 URL 的跟踪。我会给出 webroot 的完整路径,看看错误是否消失。例如:

Galleria.loadTheme('/gallery/javascript/themes/classic/galleria.classic.js');

If this don't help, try:

如果这没有帮助,请尝试:

try {
    Galleria.loadTheme('javascript/themes/classic/galleria.classic.js');
}
catch(e) {
    location.reload();
}

But this can go infinitely. I would try to get to the bottom of the error and start with different browsers to rule out a bug in your code.

但这可以无限进行。我会尝试找出错误的根源并从不同的浏览器开始,以排除代码中的错误。

回答by andre

The Beginners Guidestates that the script tag in which you load the theme needs to be afterthe images in the html source. You have probably added the script tag in the head tag. Example from the guide:

初学者指南指出,在其中您加载主题需要script标签是在HTML源图像。您可能已经在 head 标签中添加了 script 标签。指南中的示例:

<body>
    <div id="galleria">
        <img src="photo1.jpg">
        <img src="photo2.jpg">
        <img src="photo3.jpg">
    </div>
    <script>
        Galleria.loadTheme('galleria/themes/classic/galleria.classic.min.js');
        Galleria.run('#galleria');
    </script>
</body>