Javascript document.write('HTML CODE HERE')和使用flash抓取的var [英] Javascript document.write('HTML CODE HERE') and using a var grabbed by flash

查看:118
本文介绍了Javascript document.write('HTML CODE HERE')和使用flash抓取的var的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是2个问题之一。

  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>
' );

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



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



以下是函数:

 函数onSaveOk(streamName,streamDuration,userId,cameraName,micName,recorderId){
// alert(onSaveOk(+ streamName +,+ streamDuration +, +用户id +, + cameraName +, + micName +));

//用户按下记录器内的[save]按钮并返回save_video_to_db.XXX脚本save = ok
// recorderId:recorderId通过闪存变量发送,用于在同一网页上有许多录像机
$('#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>
');


}

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

$ $ p $ 'file':'onSaveOk(+ streamName +)'

我该怎么做?首先,JavaScript不允许使用多行字符串,所以请立即使用 document.write 会引发语法错误。至少有几种方法可以解决这个问题。您可以删除换行符,使整个字符串成为一行...

  var lines ='< div id = jwplayer ><中心],[...< /中心],[< / DIV>'; 
document.write(lines);

...但这往往不太可读。或者你可以用反斜线来换行...

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

...这更可读,但可能有点脆 - 任何反斜杠后面的空白将打破它而不明显。接下来,你可以连接字符串一行...

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

...哪个认为可以减轻第一个可读性问题方法和第二个脆性问题。最后,您可以创建一个字符串数组并将其加入...

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

...它具有消除重复的[完全主观]行+ = ... 。在任何情况下,这绝不是一个详尽的方法列表,它主要归结为使用任何你最喜欢的方式。

接下来,你的字符串坦率地说,这是一个巨大的单一和双引号冲突的混杂。如果您需要在字符串中包含相同类型的引号以包含该字符串,那么您需要将它们转义出来:

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

显然(?)你想把转义保持在最低限度,因为你创建一个html的字符串,你可能希望用单引号括起来,并使用双引号作为属性值。最后,上述两个问题都是导致 streamName 的原因,传递给函数的code>参数不起作用,因为在到达该行之前,您已经遇到了许多错误。如果您保持字符串问题的直接,那么

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

应该如何工作。



注意:我使用 document.write()和b)使用< center> 标签,但是您应该花些时间对这两个问题进行一点研究;我只是说1998年打电话说,这很酷,我不希望那些回来。


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?

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:

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

How would I do this? Thanks.

解决方案

First, JavaScript doesn't allow multiline strings, so right-off-the-bat your document.write is 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...

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 I think 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.

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.

And finally, both of the above issues are what are causing the streamName parameter 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

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".

这篇关于Javascript document.write('HTML CODE HERE')和使用flash抓取的var的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆