我正在尝试使用javascript动态插入一组脚本标记,其中包含一些javascript代码。我基本上是在尝试在jwplayer中包装一个文件,但是脚本字符串破坏了页面中其余的javascript代码。我怎样才能正确地做到这一点?
导致问题的线路:
$file_link_insert = "<script type='text/javascript'>jwplayer('mediaplayer').setup({flashplayer: 'player.swf', file: '"+$href+"'});</script>";ref函数的其余部分:
$(".file_link").live("click", function(e){
e.preventDefault();
var $href = $(this).attr("rel");
// Dialog
$('#filelink').dialog({
autoOpen: true,
width: 300,
modal: true,
buttons: {
"Ok": function() {
if($("input[name=file_link_text]").val()!=""){
$file_type = fileType($href);//determine if its video file see function below.
if($file_type == 'vid'){
$file_link_insert = "<script type='text/javascript'>jwplayer('mediaplayer').setup({flashplayer: 'player.swf', file: '"+$href+"'});</script>";
// $file_link_insert = " <p><a href=\""+$href+"\">"+$("input[name=file_link_text]").val()+"</a></p> ";
}else { $file_link_insert = " <p><a href=\""+$href+"\">"+$("input[name=file_link_text]").val()+"</a></p> "; }
$("#_tinyMCEinit_ifr").contents().find("body").append($file_link_insert);
$("#content_editor ul li:first a").click();
$(this).dialog("close");
$("input[name=file_link_text]").val("");
} else { alert("You must enter text label for your link!"); }
},
"Cancel": function() {
$(this).dialog("close");
}
}
});
});发布于 2012-02-13 19:51:52
您不能在javascript中包含</script>,因为浏览器会将其解释为脚本的末尾。只需拆分或转义字符串即可。像这样的<\/script>
发布于 2012-02-13 19:45:35
我认为正确的方法是使用DOM并动态加载Javascript,并将其附加到您希望的任何位置:
var script = document.createElement( 'script' );
script.type = 'text/javascript';
script.src = "myScript.js";
$("head").append( script );附加到head标记中在这种情况下,您可以将内联脚本的script.src更改为script.text。
发布于 2012-02-13 19:52:18
这是错误的:
$file_link_insert = "<script type='text/javascript'>jwplayer('mediaplayer').setup({flashplayer: 'player.swf', file: '"+$href+"'});</script>";You need to escape here or it will close the </code> element.</a>使用简单的<a href="http://mathiasbynens.be/notes/javascript-escapes">JavaScript escape sequence</a>,例如<code><\/script></code></div><pre><code>$file_link_insert = "<script>jwplayer('mediaplayer').setup({flashplayer: 'player.swf', file: '"+$href+"'});<\/script>";</code></pre><div></div>
https://stackoverflow.com/questions/9259613
复制相似问题