Javascript document.write('HTML CODE HERE') 并使用由 flash 抓取的 var

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12810454/
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-08-24 08:32:00  来源:igfitidea点击:

Javascript document.write('HTML CODE HERE') and using a var grabbed by flash

javascripthtmljwplayerrtmp

提问by CJ Sculti

so this is 2 questions in one. My first question is how would I do this?

所以这是二合一的问题。我的第一个问题是我将如何做到这一点?

    document.write('
<div id="jwplayer">
<center>
<div id='mediaplayer'></div>
<script type="text/javascript">
  jwplayer('mediaplayer').setup({
    'flashplayer': 'jwplayer/player.swf',
    'id': 'playerID',
    'width': '640',
    'height': '580',
    'provider': 'rtmp',
    'streamer': 'rtmp://domain/recorder/_definst_',
'file': 'onSaveOk("+streamName+")'
  });
</script>
</center>
</div>
');

I basically just want to print out that code when the function is called.

我基本上只想在调用函数时打印出该代码。

This is the second part of my question, this function runs when the flash video recorder finishes saving the recorded video. It grabs all of the variables in the functions arguments from the flash player. I want to use one of the variables in my jwplayer code, how would I do this?

这是我问题的第二部分,当闪存录像机完成保存录制的视频时,此功能运行。它从 Flash Player 中获取函数参数中的所有变量。我想在我的 jwplayer 代码中使用其中一个变量,我该怎么做?

Here is the function:

这是函数:

    function onSaveOk(streamName,streamDuration,userId,cameraName,micName,recorderId){
        //alert("onSaveOk("+streamName+","+streamDuration+","+userId+","+cameraName+","+micName+")");

        //the user pressed the [save] button inside the recorder and the save_video_to_db.XXX script returned save=ok
        //recorderId: the recorderId sent via flash vars, to be used when there are many recorders on the same web page
            $('#record').hide();
    document.write('
<div id="jwplayer">
<center>
<div id='mediaplayer'></div>
<script type="text/javascript">
  jwplayer('mediaplayer').setup({
    'flashplayer': 'jwplayer/player.swf',
    'id': 'playerID',
    'width': '640',
    'height': '580',
    'provider': 'rtmp',
    'streamer': 'rtmp://domain/recorder/_definst_',
'file': 'onSaveOk("+streamName+")'
  });
</script>
</center>
</div>
');


    }

This is where I try to use the streamName function, but it doesnt work:

这是我尝试使用 streamName 函数的地方,但它不起作用:

'file': 'onSaveOk("+streamName+")'

How would I do this? Thanks.

我该怎么做?谢谢。

回答by ultranaut

First, JavaScript doesn't allow multiline strings, so right-off-the-bat your document.writeis going to throw a syntax error. You have at least a couple of ways to go about it. You could just remove the line breaks, making the entire string a single line...

首先,JavaScript 不允许多行字符串,所以document.write马上就会抛出语法错误。你至少有几种方法可以解决这个问题。您可以删除换行符,使整个字符串成为一行...

var lines = '<div id="jwplayer"><center>...</center></div>';
document.write(lines);

...but that tends to not be so readable. Or you can escape the line breaks with backslashes...

...但这往往不那么可读。或者你可以用反斜杠来逃避换行符......

var lines = '<div id="jwplayer">\
    <center>\
      ...\
    </center>\
  </div>';
document.write(lines);

...which is more readable, but probably kind of brittle -- any trailing whitespace after the backslashes will break it without being apparent. Next you could concatenate the string a line at a time...

...这更具可读性,但可能有点脆弱 - 反斜杠后的任何尾随空格都会将其破坏而不明显。接下来,您可以一次将字符串连接一行...

var lines = '<div id="jwplayer">';
lines += '<center>';
lines += '...';
lines += '</center>';
lines += '</div>';
document.write(lines);

...which Ithink somewhat mitigates the readability issue of the first method and the brittleness issue of the second. Lastly, you could create an array of strings and join them...

...认为这在某种程度上减轻了第一种方法的可读性问题和第二种方法的脆弱性问题。最后,您可以创建一个字符串数组并加入它们...

var lines = [
  '<div id="jwplayer">',
  '<center>',
  '...',
  '</center>',
  '</div>'].join(' ');
document.write(lines);

...which has the [entirely subjective] advantage of eliminating the repetetive lines += .... In any case, that's by no means an exhaustive list of approaches, it mostly boils down to using whatever you're most comfortable with.

...具有消除重复的 [完全主观] 优势lines += ...。无论如何,这绝不是一个详尽的方法列表,它主要归结为使用您最喜欢的任何方法。

Next, your string is, quite frankly, a huge mish-mash of conflicting single- and double-quotes. If you need to include the same kind of quotes in your string as you're using to enclose the string, then you need to escape them:

接下来,坦率地说,你的字符串是一个巨大的单引号和双引号冲突的大杂烩。如果您需要在字符串中包含与用于包含字符串相同类型的引号,则需要对它们进行转义:

var lines = "<div id=\"jwplayer\">";
lines += '<div id=\'mediaplayer\'></div>';

Obviously(?) you'd want to keep the escapes to a minimum, and since you're creating a string(s) of html, you'd probably want to enclose those with single-quotes and use double-quotes for the attribute values. But again, it's whatever style makes the most sense to you.

显然(?)您希望将转义保持在最低限度,并且由于您正在创建一个 html 字符串,您可能希望用单引号括起来并使用双引号作为属性值。但同样,它是对您最有意义的任何风格。

And finally, both of the above issues are what are causing the streamNameparameter passed into the function not to work, since you were already running into numerous errors long before you got to that line. If you keep the string issues straight, then

最后,上述两个问题都是导致streamName传递给函数的参数不起作用的原因,因为在到达该行之前很久您就已经遇到了许多错误。如果你保持字符串问题直,那么

lines += "'file': 'onSaveOk(" + streamName + ")'";

should work just how you want it to.

应该按照你想要的方式工作。

Note:I'm foregoing the obligatory warnings about a) using document.write()and b) using <center>tags, but you should take some time to do a little research into both of those issues; I will just say that 1998 called and said, "That's cool, I didn't want those back anyhow".

注意:我放弃了关于 a) 使用document.write()和 b) 使用<center>标签的强制性警告,但是您应该花一些时间对这两个问题进行一些研究;我只想说 1998 打来电话说:“这很酷,反正我不想让那些回来”。

回答by Luigi van der Pal

Late to the party put I actually prefer:

派对迟到了,我实际上更喜欢:

//start the html

//启动html

var html = ''+
    '<body>'+
        '<div>'+
            '<a href="foo.bar">Foo Bar page</a>'+
        '</div>'+
    '</body>';

Valid javascript 'foo'+\n\r\t'bar' == 'foobar';

有效的 javascript 'foo'+\n\r\t'bar' == 'foobar';