Skip to content

Commit b516271

Browse files
committed
Fix JS
1 parent b9ec73a commit b516271

File tree

1 file changed

+50
-44
lines changed

1 file changed

+50
-44
lines changed

assets/scripts.js

Lines changed: 50 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,54 +22,60 @@ $(function() {
2222

2323
$.ajax({
2424
url: '/compile/text',
25-
dataType: 'text',
25+
dataType: 'json',
2626
data: code,
2727
method: 'POST',
2828
success: function(res) {
29-
var $iframe = $('<iframe>');
30-
31-
$('#results').empty()
32-
.append($('<h2>').append('Compiled Result'))
33-
.append($iframe);
34-
35-
$('html, body').animate({
36-
scrollTop: $iframe.offset().top
37-
}, 500);
29+
if (res.error) {
30+
$('#results').empty()
31+
.append($('<h2>').append('Error'))
32+
.append($('<pre>').append($('<code>').append(res.error)));
33+
} else if (res.js) {
34+
var $iframe = $('<iframe>');
3835

39-
var iframe = $iframe.get(0).contentWindow.document;
40-
41-
iframe.open();
42-
iframe.write(
43-
[ '<html>'
44-
, ' <head>'
45-
, ' <title>Try PureScript</title>'
46-
, ' </head>'
47-
, ' <body>'
48-
, ' <div id="console"></div>'
49-
, ' </body>'
50-
, '</html>'
51-
].join('\n'));
36+
$('#results').empty()
37+
.append($('<h2>').append('Compiled Result'))
38+
.append($iframe);
39+
40+
$('html, body').animate({
41+
scrollTop: $iframe.offset().top
42+
}, 500);
5243

53-
var initScript =
54-
[ 'var console = {'
55-
, ' log: function(s) {'
56-
, ' var text = document.createTextNode(s);'
57-
, ' var code = document.createElement("code");'
58-
, ' var div = document.createElement("div");'
59-
, ' div.appendChild(code);'
60-
, ' code.appendChild(text);'
61-
, ' document.getElementById("console").appendChild(div);'
62-
, ' }'
63-
, '};'
64-
].join('\n');
65-
66-
var scripts = [initScript, res];
67-
68-
for (var i = 0; i < scripts.length; i++) {
69-
var script = iframe.createElement('script');
70-
script.appendChild(iframe.createTextNode(scripts[i]));
71-
var head = iframe.getElementsByTagName('head')[0];
72-
head.appendChild(script);
44+
var iframe = $iframe.get(0).contentWindow.document;
45+
46+
iframe.open();
47+
iframe.write(
48+
[ '<html>'
49+
, ' <head>'
50+
, ' <title>Try PureScript</title>'
51+
, ' </head>'
52+
, ' <body>'
53+
, ' <div id="console"></div>'
54+
, ' </body>'
55+
, '</html>'
56+
].join('\n'));
57+
58+
var initScript =
59+
[ 'var console = {'
60+
, ' log: function(s) {'
61+
, ' var text = document.createTextNode(s);'
62+
, ' var code = document.createElement("code");'
63+
, ' var div = document.createElement("div");'
64+
, ' div.appendChild(code);'
65+
, ' code.appendChild(text);'
66+
, ' document.getElementById("console").appendChild(div);'
67+
, ' }'
68+
, '};'
69+
].join('\n');
70+
71+
var scripts = [initScript, res.js];
72+
73+
for (var i = 0; i < scripts.length; i++) {
74+
var script = iframe.createElement('script');
75+
script.appendChild(iframe.createTextNode(scripts[i]));
76+
var head = iframe.getElementsByTagName('head')[0];
77+
head.appendChild(script);
78+
}
7379
}
7480
},
7581
error: function(res) {
@@ -79,4 +85,4 @@ $(function() {
7985
}
8086
});
8187
});
82-
});
88+
});

0 commit comments

Comments
 (0)