执行在RoR中通过ajax请求返回的javascript

时间:2014-03-06 10:07:28

标签: javascript jquery ruby-on-rails ajax

我有这样的rails渲染javascript视图:

$("##{@organization.id}-organization-introtext").hide();
$("##{@organization.id}-organization-full").append("#{escape_javascript(render @organization)}").hide().fadeIn('slow').focus();

并通过它,例如我得到这样的数据:

$("#9-organization-introtext").hide();
$("#9-organization-full").append("<h2 id=\'magenta\'>\n  Shell - Автосервис\n<\/h2>\n<div>\n  Shell - Автосервис\n  <br>\n  <br>\n<\/div>\n<div>\n  <strong>\n    Контактная информация:\n  <\/strong>\n  <p>\n    8 (0152) 510 884\n    8 (029) 265 38 09\n  <\/p>\n  <p>\n    <script type=\"text/javascript\" charset=\"utf-8\" src=\"//api-maps.yandex.ru/services/constructor/1.0/js/?sid=SrxOH8zh15XLRmFVDc91yCeDQr23_VUl&width=600&height=450\"><\/script>\n  <\/p>\n  <p>\n    г.Ростов-на-Дону, ул.Василевского 1\n  <\/p>\n<\/div>\n<div class=\'tags\'>\n  <span>\n    Метки:\n  <\/span>\n  автозапчасти, замена масла\n<\/div>\n").hide().fadeIn('slow').focus();

但是在浏览器中这个脚本没有执行,为什么?怎么运行呢?并显示这个yandex.map?

1 个答案:

答案 0 :(得分:0)

@organization拆分Javascript代码和HTML代码,并将Javascript代码添加到文档的head部分。

var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.text = "#{{escape_javascript(render @organization_javascript)}";
$("head").append( script );

来源 - Can't append <script> element