1

The code below works as is. I want to use this code to display specific Twitch streams based on the page that is loaded.

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

When I try to replace

"Defatank's Live Stream Page"

with the object below it doesn't work and breaks the rest of the cases.

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

I'm sure I'm probably using document.write wrongly or should be using something entirely different. Any suggestions?

EDIT: Here is what the finished product looks like.

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

I also tried removing the "" from document.write() and it still dont load and breaks the rest of the cases.


How to create unscubscribe functionality to let user unsubscribe from in-app subscription purchase?

I am building code to let a user subscribe to an in-app purchase subscription.

But I do not see any tutorials or examples (even from the official site) to let the user unsubscribe. Could anyone please point me to a tutorial for this, or please explain how users can unsubscribe from my in-app subscriptions?

Thank you@

4

4 回答 4

2

For your specific case the easiest fix (in my opinion) is to change the double quotes for the attributes in object to single ones (else it ends the document.write string). This should work

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

The JavaScript interpreter needs to know where a string begins and ends and the quote marks are used to do that. You can use single or double quotes for JavaScript strings. If you want to use the same quote type in a string that you used to deliminate the string, then that quote characters must be escaped - otherwise the interpreter gets confused and thinks the string has ended early.

Some examples

1. "Good string"
1. "Broken "hi" string"
1. "Good 'hi' string"
1. "Good \"hi\" string"

The 2nd string will not work because the interpreter thinks it ended just before the word hi - as indicated by the double quotes.

The 3rd string is fine because it uses single quotes for hi instead.

The 4th string is also fine because it "escapes" the double quote characters to let the interpreter know the following character should be treated literally and not as JavaScript code.

There's a little more details on JavaScript strings here: http://www.w3schools.com/js/js_obj_string.asp

So in summary, as the HTML spec allows for single, double, or not quoted attributes, changing the double quotes to single resolves the issue.

于 2013-08-09T22:57:07.860 回答
2

You need to escape the quotation marks inside the string if you use quotation marks as delimiters. Usually I use apostropes to delimit a string that has quotation marks.

(Note however that the & characters in the attributes should be HTML encoded as &amp; to make it correct HTML code.)

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&amp;channel=defatank&amp;auto_play=true&amp;start_volume=25" /></object>');
于 2013-08-09T22:59:21.033 回答
0

根据http://www.w3schools.com/jsref/met_doc_write.asp

The write() method writes HTML expressions or JavaScript code to a document.

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

于 2013-08-09T22:53:25.977 回答
0

你肯定不想用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);
于 2013-08-09T23:05:12.240 回答