0

I want to use a variable that I define before in an object. I have the following code:

<object type="application/x-shockwave-flash" height="378" width="800" data="//www-cdn.jtvnw.net/swflibs/TwitchPlayer.swf" bgcolor="#000000">
    <param name="allowFullScreen" value="false" />
    <param name="allowScriptAccess" value="always" />
    <param name="allowNetworking" value="all" />
    <param name="movie" value="//www-cdn.jtvnw.net/swflibs/TwitchPlayer.swf" />
    <param name="flashvars" value="channel=" + user_channelname + "&auto_play=true&start_volume=25" />
</object>

I basically want to insert user_channelname as the value for the "channel="

dhuang
  • 909
  • 4
  • 18
Splinti
  • 385
  • 3
  • 13
  • 2
    Well you can't do that... you need to set that attribute in the actual JS code... or use some server code to write it in. – tymeJV Aug 14 '15 at 16:17
  • Why is JS so complex :( Sorry but I am a complete noob and I only wanted to put in a variable... why is that so hard in JS :D – Splinti Aug 14 '15 at 16:18
  • 1
    The complicated part here is that browsers separate the DOM (HTML) and scripts (JS). That's an artifact of HTML existing before JS did. – ssube Aug 14 '15 at 16:20

1 Answers1

1

You could use Javascript for that:

If you set an ID to your <param>

<param id="flashvars" name="flashvars" /> <!--would not recommend; see below-->
<script>
    window.onload = function () {
        var user_channelname = "What you want your variable to be."
        document.getElementById("flashvars").value = "channel=" + user_channelname + "&auto_play=true"
    }
</script>

Explanation: document.getElementById accesses (first) element with a specific id. The .value allows you to either grab the value or change it like above. See more here: http://www.w3schools.com/jsref/met_doc_getelementbyid.asp.
For more on window.onload, see here https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload. But basically when the window loads, it will automatically set your variable (before there was nothing "calling" the document.getElementById function.


Post Script: It seems to me that you are using the name field of your elements as a way to identify them. However, in Javascript it tends to be easier (and make more sense; think unique ID cards. i.e. drivers' license) if you used the id field instead: e.g.
<object type="application/x-shockwave-flash" height="378" width="800" data="//www-cdn.jtvnw.net/swflibs/TwitchPlayer.swf" bgcolor="#000000">
    <param id="allowFullScreen" value="false" /><param name="allowScriptAccess" value="always" />
    <param id="allowNetworking" value="all" />
    <param id="movie" value="//www-cdn.jtvnw.net/swflibs/TwitchPlayer.swf" />
    <param id="flashvars" value="channel=" + user_channelname + "&auto_play=true&start_volume=25" />
</object>
dhuang
  • 909
  • 4
  • 18
  • I've updated my question, are you able to applay it to that ? – Splinti Aug 14 '15 at 16:22
  • i cant edit this i think cause this is some kind of embedding. I'm embedding the twitch player and the `user_channelname` is saying which channel to embed.... So I cant modify these `name` fields i think – Splinti Aug 14 '15 at 16:40
  • Can you add `id` fields? If not you could try using `document.getElementsByName("flashvars")[0].value = ...` See http://stackoverflow.com/questions/10306129/javascript-get-element-by-name – dhuang Aug 14 '15 at 16:42
  • ok i could change them but it still wont work. I don't understand Javascript at all and I have no clue why it is so hard so just assign a variable. In other languages i Learned its just like `y = 2`. Why does it have to be so hard in Javascript. – Splinti Aug 14 '15 at 16:47
  • Do you want the variable to be set as soon as the window loads? (And it's hard because HTML and Javascript are two different things. Javascript is the language, but it's displayed in HTML. Normally, languages like Python and pygame are written and displayed with python.) – dhuang Aug 14 '15 at 16:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/87015/discussion-between-aznbanana9-and-splinti). – dhuang Aug 14 '15 at 16:57