I'm not sure if this is best practice or the complete wrong way to do this but I am trying to dynamically generate some functions and use them on button clicks. The buttons are also dynamically generated.
I am making a twitch stream switcher for my guild website and instead of adding more and more code manually for each streamer that signs up I want to just add a single function call. right now I have this:
<script id="buttonScripts">
twitchStatus('player1');
twitchStatus('player2');
twitchStatus('player3');
function twitchStatus(twitchName){
$.getJSON('https://api.twitch.tv/kraken/streams/' + twitchName, function(streamData){
if (streamData.stream == null){
$(twitchButtons).append('<button type="button" class="btn btn-fill btn-round" onclick="' + twitchName + 'Function();">' + twitchName + '\'s Stream<img src="images/toffline.png"></button>');
$(buttonScripts).append('function ' + twitchName + 'Function() {var StreamObject = document.getElementById("live_embed_player_flash");if (StreamObject != null) {StreamObject.setAttribute(\'data\', \'http://www.twitch.tv/widgets/live_embed_player.swf?channel=' + twitchName + ');}}');
} else {
$(twitchButtons).append('<button type="button" class="btn btn-fill btn-round" onclick="' + twitchName + 'Function();">' + twitchName + '\'s Stream<img src="images/tonline.png"></button>');
$(buttonScripts).append('function ' + twitchName + 'Function() {var StreamObject = document.getElementById("live_embed_player_flash");if (StreamObject != null) {StreamObject.setAttribute(\'data\', \'http://www.twitch.tv/widgets/live_embed_player.swf?channel=' + twitchName + ');}}');
}
})
};
</script>
The script generates the button and function if it detects them on or offline but uses a different png to show the status. The function itself modifies the embed object to replace the URL for the stream with the selected streamers url and it works perfectly when it is not generated through this function.
The generated functions are appended to the bottom of the script so they live in the same set of script tags. Is there something i am missing that would prevent them from being accessible to the onclick call from the buttons?