Splinti
Splinti

Reputation: 385

Using variables in Javascript objects

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

Upvotes: 0

Views: 86

Answers (1)

dhuang
dhuang

Reputation: 921

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>

Upvotes: 1

Related Questions