Javascript 使用document.write和<;对象>;

Javascript 使用document.write和<;对象>;,javascript,Javascript,下面的代码按原样工作。我想使用此代码根据加载的页面显示特定的Twitch流 <script type="text/javascript"> var stream = location.pathname; switch (stream) { case "/defatank": document.write("Defatank's Live Stream Page"); break; case "/see

下面的代码按原样工作。我想使用此代码根据加载的页面显示特定的Twitch流

<script type="text/javascript">
    var stream = location.pathname;
    switch (stream) {
       case "/defatank":
          document.write("Defatank's Live Stream Page");
          break;
       case "/seeingblue":
          document.write("SeeingBlue's Live Stream Page");
          break;
       case "/shiroshii":
          document.write("Shiroshii's Live Stream Page");
          break;
       case "/theend66":
          document.write("TheEnd66's Live Stream Page");
          break;
       case "/wakawaka647":
          document.write("WakaWaka647's Live Stream Page");
          break;
       case "/xtheguythatplays":
          document.write("XTheGuyThatPlays' Live Stream Page");
          break;
    }
    </script>

var stream=location.pathname;
交换机(流){
箱子“/油箱”:
文件。书写(“Defatank的实时流页面”);
打破
案例“/见蓝色”:
文件。写(“SeeingBlue的直播页面”);
打破
案例“/shiroshii”:
文件。书写(“Shiroshii的直播页面”);
打破
案例“/theend66”:
文件。书写(“TheEnd66的直播页面”);
打破
案例“/wakawaka647”:
编写(“WakaWaka647的直播页面”);
打破
案例“/X扮演的人”:
文档。写(“第X个播放直播页面的家伙”);
打破
}
当我试图替换

“Defatank的实时流页面”

对于下面的对象,它不起作用,并破坏了其余的案例

<object type="application/x-shockwave-flash" height="378" width="620" id="live_embed_player_flash" data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=defatank" bgcolor="#000000"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="movie" value="http://www.twitch.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="hostname=www.twitch.tv&channel=defatank&auto_play=true&start_volume=25" /></object>

我肯定我可能使用了document.write错误,或者应该使用完全不同的东西。有什么建议吗

编辑: 这是成品的样子

<script type="text/javascript">
    var stream = location.pathname;
    switch (stream) {
       case "/defatank":
          document.write("<object type="application/x-shockwave-flash" height="378" width="620" id="live_embed_player_flash" data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=defatank" bgcolor="#000000"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="movie" value="http://www.twitch.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="hostname=www.twitch.tv&channel=defatank&auto_play=true&start_volume=25" /></object>");
          break;
       case "/seeingblue":
          document.write("SeeingBlue's Live Stream Page");
          break;
       case "/shiroshii":
          document.write("Shiroshii's Live Stream Page");
          break;
       case "/theend66":
          document.write("TheEnd66's Live Stream Page");
          break;
       case "/wakawaka647":
          document.write("WakaWaka647's Live Stream Page");
          break;
       case "/xtheguythatplays":
          document.write("XTheGuyThatPlays' Live Stream Page");
          break;
    }
    </script>

var stream=location.pathname;
交换机(流){
箱子“/油箱”:
文件。填写(“”);
打破
案例“/见蓝色”:
文件。写(“SeeingBlue的直播页面”);
打破
案例“/shiroshii”:
文件。书写(“Shiroshii的直播页面”);
打破
案例“/theend66”:
文件。书写(“TheEnd66的直播页面”);
打破
案例“/wakawaka647”:
编写(“WakaWaka647的直播页面”);
打破
案例“/X扮演的人”:
文档。写(“第X个播放直播页面的家伙”);
打破
}
我还尝试从document.write()中删除“”,但它仍然无法加载并破坏了其余的案例。

根据

<object type="application/x-shockwave-flash" height="378" width="620" id="live_embed_player_flash" data="http://www.twitch.tv/widgets/live_embed_player.swf?channel=defatank" bgcolor="#000000"><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="movie" value="http://www.twitch.tv/widgets/live_embed_player.swf" /><param name="flashvars" value="hostname=www.twitch.tv&channel=defatank&auto_play=true&start_volume=25" /></object>
这意味着您不能将DOMObject或常规对象传递到document.write。如果您列出的“对象”实际上是一个字符串,那么您可以将其传递到document.write中。如果您试图向页面添加DOMObject,请尝试使用
document.body.addChild(object)

根据


这意味着您不能将DOMObject或常规对象传递到document.write。如果您列出的“对象”实际上是一个字符串,那么您可以将其传递到document.write中。如果您试图向页面添加DOMObject,请尝试使用
document.body.addChild(object)

对于您的特定情况,最简单的修复方法(在我看来)是将对象中属性的双引号更改为单引号(否则它将结束document.write字符串)。这应该行得通

<script type="text/javascript">
    var stream = location.pathname;
    switch (stream) {
       case "/defatank":
          document.write("<object type='application/x-shockwave-flash' height='378' width='620' id='live_embed_player_flash' data='http://www.twitch.tv/widgets/live_embed_player.swf?channel=defatank' bgcolor='#000000'><param name='allowFullScreen' value='true' /><param name='allowScriptAccess' value='always' /><param name='allowNetworking' value='all' /><param name='movie' value='http://www.twitch.tv/widgets/live_embed_player.swf' /><param name='flashvars' value='hostname=www.twitch.tv&channel=defatank&auto_play=true&start_volume=25' /></object>");
          break;
       case "/seeingblue":
          document.write("SeeingBlue's Live Stream Page");
          break;
       case "/shiroshii":
          document.write("Shiroshii's Live Stream Page");
          break;
       case "/theend66":
          document.write("TheEnd66's Live Stream Page");
          break;
       case "/wakawaka647":
          document.write("WakaWaka647's Live Stream Page");
          break;
       case "/xtheguythatplays":
          document.write("XTheGuyThatPlays' Live Stream Page");
          break;
    }
</script>
第二个字符串将不起作用,因为解释器认为它在单词hi-之前结束,如双引号所示

第三个字符串很好,因为它使用单引号表示hi

第四个字符串也可以,因为它“转义”了双引号字符,让解释器知道下面的字符应该按字面意思处理,而不是作为JavaScript代码处理

这里有关于JavaScript字符串的更多细节:


总之,由于HTML规范允许使用单引号、双引号或不带引号的属性,因此将双引号更改为单引号可以解决问题。

对于您的特定情况,最简单的解决方法(在我看来)是将object中属性的双引号更改为单引号(否则将结束document.write字符串)。这应该行得通

<script type="text/javascript">
    var stream = location.pathname;
    switch (stream) {
       case "/defatank":
          document.write("<object type='application/x-shockwave-flash' height='378' width='620' id='live_embed_player_flash' data='http://www.twitch.tv/widgets/live_embed_player.swf?channel=defatank' bgcolor='#000000'><param name='allowFullScreen' value='true' /><param name='allowScriptAccess' value='always' /><param name='allowNetworking' value='all' /><param name='movie' value='http://www.twitch.tv/widgets/live_embed_player.swf' /><param name='flashvars' value='hostname=www.twitch.tv&channel=defatank&auto_play=true&start_volume=25' /></object>");
          break;
       case "/seeingblue":
          document.write("SeeingBlue's Live Stream Page");
          break;
       case "/shiroshii":
          document.write("Shiroshii's Live Stream Page");
          break;
       case "/theend66":
          document.write("TheEnd66's Live Stream Page");
          break;
       case "/wakawaka647":
          document.write("WakaWaka647's Live Stream Page");
          break;
       case "/xtheguythatplays":
          document.write("XTheGuyThatPlays' Live Stream Page");
          break;
    }
</script>
第二个字符串将不起作用,因为解释器认为它在单词hi-之前结束,如双引号所示

第三个字符串很好,因为它使用单引号表示hi

第四个字符串也可以,因为它“转义”了双引号字符,让解释器知道下面的字符应该按字面意思处理,而不是作为JavaScript代码处理

这里有关于JavaScript字符串的更多细节:


总之,由于HTML规范允许使用单引号、双引号或不带引号的属性,因此将双引号更改为单引号可以解决问题。

如果使用引号作为分隔符,则需要转义字符串中的引号。通常我使用撇号来分隔带引号的字符串

(但是请注意,属性中的
&
字符应采用HTML编码为
&
,以使其成为正确的HTML代码。)

document.write(“”);

如果使用引号作为分隔符,则需要转义字符串中的引号。通常我使用撇号来分隔带引号的字符串

(但是请注意,属性中的
&
字符应采用HTML编码为
&
,以使其成为正确的HTML代码。)

document.write(“”);

您肯定不想使用
文档。请编写
。相反,请使用DOM方法创建和附加元素:

var obj = document.createElement('object'),
    param = document.createElement('param');
object.setAttribute('type', 'application/x-shockwave-flash');
param.setAttribute('name', 'allowFullScreen');
obj.appendChild(param);
// rest of params and attributes
document.body.appendChild(obj);

您肯定不想使用
文档。请编写
。相反,请使用DOM方法创建和附加元素:

var obj = document.createElement('object'),
    param = document.createElement('param');
object.setAttribute('type', 'application/x-shockwave-flash');
param.setAttribute('name', 'allowFullScreen');
obj.appendChild(param);
// rest of params and attributes
document.body.appendChild(obj);

您究竟是如何使用
?如果操作正确,您所拥有的应该可以使用。你能展示你写标签而不是文本的尝试吗?我猜你需要将对象中属性的双引号改为单引号(否则它会结束document.write字符串)。不要使用
document.write
-就是这样archaic@JoshLs用单引号括起来:
document.write(“”)您究竟是如何尝试使用
?如果操作正确,您所拥有的应该可以工作。你能展示你写标签而不是文本的尝试吗?我猜你需要将object中属性的双引号改为单引号(否则它会结束document.write字符串)。不要使用