1

How can I include this varibale without it shooting me an error

The variable is vid on the vars line

$('.playVideo').live('click',function(z){
            z.preventDefault();//Disable Default Method
            var vid = $(this).attr('data');

....

$('#video').empty().flash({
    "src":"video/videoplayer-loop.swf",
    "width":322,
    "height":275,
    "vars":{"image":"img/video-posterframe-product.jpg","videoLink=player/video/"+vid+".flv","hideAudio1":"false","videoVolume":"50"},

});

I get thrown a

Uncaught SyntaxError: Unexpected identifier

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
Tom
  • 761
  • 3
  • 15
  • 33

2 Answers2

2

The vid variable is out of scope, you need to either put these two bits of code in the same function:

$('.playVideo').live('click',function(z){
    z.preventDefault();//Disable Default Method
    var vid = $(this).attr('data');

    $('#video').empty().flash({
        "src":"video/videoplayer-loop.swf",
        "width":322,
        "height":275,
        "vars":{"image":"img/video-posterframe-product.jpg", "videoLink=player/video/" + vid + ".flv", "hideAudio1": "false", "videoVolume": "50"},
    });
});

Or put the code which sets up the flash into it's own function and call it from your click handler, passing the vid variable:

$('.playVideo').live('click',function(z){
    z.preventDefault();//Disable Default Method
    var vid = $(this).attr('data');
    setUpFlash(vid);
});

function setUpFlash(video) {
    $('#video').empty().flash({
        "src":"video/videoplayer-loop.swf",
        "width":322,
        "height":275,
        "vars":{"image":"img/video-posterframe-product.jpg", "videoLink=player/video/" + video + ".flv", "hideAudio1": "false", "videoVolume": "50"},
    });
});

You could also declare the vid variable globally, but this is not best practice.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • why you say it's not best practice to declare 'vid' globally? – Khalizar Apr 12 '12 at 08:02
  • 1
    See the top answer in this question - it explains it better than I could: http://stackoverflow.com/questions/5063878/javascript-global-variables-best-practices – Rory McCrossan Apr 12 '12 at 08:05
  • I am getting the error Uncaught SyntaxError: Unexpected token + when trying the setup above – Tom Apr 12 '12 at 08:27
  • on line 119. Here is the script. http://www.pavlishgroup.com/projects/tumbltrak/ – Tom Apr 14 '12 at 22:54
0

try to declare var vid outside any function

var vid;
$('.playVideo').live('click',function(z){
    z.preventDefault();//Disable Default Method
    vid = $(this).attr('data');
    ....
}
Khalizar
  • 307
  • 1
  • 3
  • 13