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

徘徊边缘 提交于 2019-12-04 15:21:21

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

Late to the party put I actually prefer:

//start the html

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!