Trying to use a javascript variable in html - please help!

Discussion in 'JavaScript' started by katef1, Oct 29, 2009.

  1. #1
    :confused:
    Hi all,

    I'm very new to Javascript and am probably having difficulty with something thats actually very easy! I've been trying different things for the last couple of hours but nothing seems to be working. Basically I want to play a selection of different videos via one page. Each different thumbnail a user clicks on would lead to a different video playing in the main area of my page.

    As a said I've tried a few different things but this is my latest effort:

    <html>
    <title>Videos</title>

    <head>

    <script type="text/javascript">

    var vid = http://www.youtube.com/v/wX3sv9N-zOA&hl=en&fs=1&;

    </script>

    </head>

    <body>

    <table>

    <tr><td><img src="images/training_video_pic" height="80"></td>

    <td rowspan="3">
    <object width="425" height="344"><script>document.write('<PARAM name="SRC" VALUE="'+vid+'">')</script></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><script>document.write('<embed src="'+vid+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344">')</script></embed></object>
    </td>
    </tr>

    <tr>
    <td><img src="images/training_video_pic" height="80"></td>
    </tr>

    <tr>
    <td><img src="images/training_video_pic" height="80"></td>
    </tr>

    </table>


    At the moment it's displying a white box but no video. I would obviously then have to go and write some 'if' statements to get the functionality working.

    Any help/suggestions would be greatly appreciated! Thank you in adavnce.

    Kate
     
    katef1, Oct 29, 2009 IP
  2. parantox

    parantox Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello,

    If you are using some function to embed the video to your page and need the URL of the video, it is probably passed as a string argument, so

    var vid = "http://www.youtube.com/v/wX3sv9N-zOA&hl=en&fs=1&;" should store the url to a variable named vid.

    Right now it's definitely not going to work because the value assignment is wrong.

    Hope that was helpful.
     
    parantox, Oct 29, 2009 IP
  3. katef1

    katef1 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3


    Hi,

    Thanks for replying. Unfortunately however it's still not working. Can I just check: is it supposed to be "http://www.youtube.com/v/wX3sv9N-zOA&hl=en&fs=1&;" or "http://www.youtube.com/v/wX3sv9N-zOA&hl=en&fs=1&" ;

    I've declared the variable in the head section. Is this right. I've got a feeling I'm close but not close enough!

    Thanks,

    Kate
     
    katef1, Oct 29, 2009 IP
  4. chadsmith

    chadsmith Peon

    Messages:
    82
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I see a few reasons why it's not working.

    First:

    var vid = http://www.youtube.com/v/wX3sv9N-zOA&hl=en&fs=1&;
    Code (markup):
    Should be

    var vid = 'http://www.youtube.com/v/wX3sv9N-zOA&hl=en&fs=1&';
    Code (markup):
    and second:

    The script should write the entire object element rather than just parts of it. This is because the DOM content (including <object> and <embed>) is loaded first before the JavaScript runs so the browser is ending up with broken elements.

    That part should be:

    <script type="text/javscript">document.write('<object width="425" height="344"><PARAM name="SRC" VALUE="'+vid+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+vid+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>');</script>
    HTML:
     
    chadsmith, Oct 29, 2009 IP
  5. parantox

    parantox Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    First of all it's a good habit to always put a semicolon at the end of each statement in javascript. I thought it was necessary but I've tested it and it still works without one (part of the error tolerance of the language).

    I've also checked the link and it doesn't make a difference whether you have a semicolon or not, it works.

    In general, you just type the link as you see it.

    As for the declaration of the variable, it's all right where it is.

    Maybe these will help:
    http://www.tizag.com/htmlT/htmlvideocodes.php
    http://www.tutorialized.com/view/tutorial/Embed-Video/3490
     
    parantox, Oct 29, 2009 IP