如何加载ace编辑器

时间:2013-03-24 14:22:04

标签: javascript html cdn code-editor

我正在尝试使用Ace代码编辑器库(http://ace.ajax.org/),但我遇到了麻烦。根据嵌入指南,这应该从Amazons CDN加载所需的js文件。

<script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>

然而它失败了,在Chromes控制台中显示:

Could not load worker ace.js:1
DOMException {message: "SecurityError: DOM Exception 18", name: "SecurityError", code: 18, stack: "Error: An attempt was made to break through the se…cloudfront.net/src-min-noconflict/ace.js:1:76296)", INDEX_SIZE_ERR: 1…}
 ace.js:1

我还尝试在本地放置ace库src-min文件夹并使用

加载它
<script src="/js/ace/ace.js" type="text/javascript" charset="utf-8"></script>

错误也失败了:

Uncaught RangeError: Maximum call stack size exceeded
GET http://mysite/mode-javascript.js 404 (Not Found) 123f2c9_ace_1.js:1
GET http://mysite/notes/theme-monokai.js 404 (Not Found) 123f2c9_ace_1.js:1
Uncaught RangeError: Maximum call stack size exceeded

最后,我尝试加载ace src-min文件夹中的所有js资源,该文件夹也因错误而失败:S

4 个答案:

答案 0 :(得分:9)

我无法将所有代码粘贴到评论中,因此我将通过更新此问题开始回答您的问题。这对我来说很好:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>HTML</title>
    <style>
        #editor { 
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
            }
    </style>
</head>
<body>
    <div id="editor">
        function foo(items) {
            var x = "All this is syntax highlighted";
            return x;
        }
    </div>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="utf-8"></script>
    <script>
        var editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai");
        editor.getSession().setMode("ace/mode/javascript");
    </script>
</body>
</html>

你可以在最后测试一下,看看你是否仍然遇到问题?如果这个代码(单数)没问题,那么其他一些JavaScript可能会影响ACE。

这是一个JSfiddle:http://jsfiddle.net/yDscY/。我的开发检查员没有错误。

我发现了问题。如果你有PHP或者可以使用.htaccess。您必须发送特定标头以符合CORS(跨源资源共享)。

access-control-allow-origin: *
access-control-allow-credentials: true

之后它应该有用。

<强> 更新

我没有彻底测试这部分,但它应该有用。

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Credentials: "true"
</IfModule>
祝你好运!

答案 1 :(得分:3)

正确答案在第一条评论中:

  

试试这个editor.getSession()。setUseWorker(false);看看它是否还在   失败。本地将无法正常工作,因为它依赖于其他在线   相关文件。这就是相对GET失败的原因。我不是   使用第一个在线链接获取任何错误。也许是什么   否则正在打断你的JavaScript?你能展示一个更完整的版本吗?   你的HTML / JS文件?

- Allendar ,3月24日14:25

答案 2 :(得分:1)

我试图完成这件事时遇到了麻烦。 ACE主页中给出的代码对我不起作用。我将所有文件都放在本地目录中,但如果需要,可以使用CDN。

我将lib/ace的ace目录放入我的static/目录。将该部分更改为您自己的位置。

我不得不使用Require.js api来加载ace。这是对我有用的代码:

<!DOCTYPE html>
<html lang="en">
<head>
<title>ACE in Action</title>
<style type="text/css" media="screen">
    #editor { 
        position: absolute;
        top: 0;
        right: 0;
        bottom: 0;
        left: 0;
    }
</style>
</head>
<body>

<div id="editor">
    function foo(items) {
    var x = "All this is syntax highlighted";
    return x;
    }
</div>
<script type="text/javascript" src="/static/require.js"></script>
<script>
    require.config({
        baseUrl: window.location.protocol + "//" + window.location.host
            + window.location.pathname.split("/").slice(0, -1).join("/"),

        paths: {
            ace: "/static/ace"
        }
    });

    require(["ace/ace"], function (ace) {
        var editor = ace.edit("editor");
        editor.setTheme("ace/theme/monokai");
        editor.getSession().setMode("ace/mode/javascript");
    });
</script>
</body>
</html>

来源:https://github.com/ajaxorg/ace/issues/1017

如果您遇到一些疯狂的错误,请查看此页面:http://requirejs.org/docs/errors.html

答案 3 :(得分:0)

我知道这不会完全回答你的问题,但是我正在为这个登陆此页并且遇到同样问题的人写这篇文章

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
    Header set Access-Control-Allow-Credentials: "true"
</IfModule>

editor.getSession().setUseWorker(false);

不起作用。

我在Chrome上遇到了同样的问题。我在Firefox和Opera中测试了我的网站,它按预期工作。加载页面时,我一直收到Uncaught RangeError: Maximum call stack size exceeded错误。

解决方案是清空Chrome的缓存并再次运行。即使是control/command + shift + rcontrol + F5也行不通。我真的不得不进入设置并清空缓存。

同样,我知道这只是部分相关,但这适用于登陆此页面的其他人!