javascript - Using document.write with an <object> -


the code below works is. want use code display specific twitch streams based on page 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 try replace

"defatank's live stream page"

with object below doesn't work , breaks rest of 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 using document.write wrongly or should using entirely different. suggestions?

edit: here 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 tried removing "" document.write() , still dont load , breaks rest of cases.

for specific case easiest fix (in opinion) change double quotes attributes in object single ones (else ends document.write string). 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 know string begins , ends , quote marks used that. can use single or double quotes javascript strings. if want use same quote type in string used deliminate string, quote characters must escaped - otherwise interpreter gets confused , thinks string has ended early.

some examples

1. "good string" 1. "broken "hi" string" 1. "good 'hi' string" 1. "good \"hi\" string" 

the 2nd string not work because interpreter thinks ended before word hi - indicated double quotes.

the 3rd string fine because uses single quotes hi instead.

the 4th string fine because "escapes" double quote characters let interpreter know following character should treated literally , not javascript code.

there's little more details on javascript strings here: http://www.w3schools.com/js/js_obj_string.asp

so in summary, html spec allows single, double, or not quoted attributes, changing double quotes single resolves issue.


Comments

Popular posts from this blog

css - Which browser returns the correct result for getBoundingClientRect of an SVG element? -

gcc - Calling fftR4() in c from assembly -

Function that returns a formatted array in VBA -