Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>See this fiddle: <a href="http://jsfiddle.net/Y9j7R/5/" rel="nofollow noreferrer">http://jsfiddle.net/Y9j7R/5/</a></p> <p>Run this code on load:</p> <pre><code>var a = document.getElementsByTagName("a"); //1 for(var i=0; i&lt;a.length; i++){ //2 if(!/#ytplayer/.test(a[i].href)) continue; //3 var link = a[i].innerHTML.match(/\/vi\/([^\/]+)/); //4 if(link) (function(vidId){ //5 a[i].onclick = function(){ //6 player.loadVideoById(vidId); //7 } //8 })(link[1]); //9 } </code></pre> <h2>Detailed explanation of the code</h2> <ol> <li>Select all <code>&lt;a&gt;</code> (anchor) elements in the document</li> <li>Loop through these anchors using <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/for" rel="nofollow noreferrer"><code>for</code></a></strong>. During each iteration, the "current" anchor is referred through <code>a[i]</code>.</li> <li>Check whether the <code>href</code> attribute does <em>not</em> (<code>!</code>) contain "<code>#ytplayer</code>" using the <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/test" rel="nofollow noreferrer"><code>test</code></a></strong> method of the <strong><a href="https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions" rel="nofollow noreferrer">Regular Expression</a></strong>. If this condition is true (ie: the <code>href</code> attribute does not contain "<code>#ytplayer</code>"), the <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/continue" rel="nofollow noreferrer"><code>continue</code></a></strong> statement terminates the current iteration, and jumps to the next anchor.</li> <li>The <strong><a href="https://developer.mozilla.org/en/DOM/element.innerHTML" rel="nofollow noreferrer"><code>innerHTML</code></a></strong> property of the current anchor is requested. The <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match" rel="nofollow noreferrer"><code>match</code></a></strong> method is used to get the video id. The <a href="https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions" rel="nofollow noreferrer">Regular expression</a> <code>/\/vi\/([^\/]+)/</code> means: match a substring which equals <em><code>/vi/&lt;any consecutive non-slash chars&gt;</code></em>, and group <code>&lt;any consecutive non-slash chars&gt;</code>.<br /> When such a substring is found, the <code>link</code> variable has a property <code>1</code> (one), which holds the value of this group. Otherwise, <code>link</code> equals <code>null</code>.</li> <li>If the <code>link</code> variable is not <code>null</code>, an anonymous <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/function" rel="nofollow noreferrer"><code>function</code></a></strong> is created (lines 5-9) and executed (line 9). The first argument of the function will be referenced through <em><code>vidId</code></em> (variable).</li> <li>Assigns a newly created <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/function" rel="nofollow noreferrer"><code>function</code></a></strong> to the <code>onclick</code> property of the current anchor. Assigning a function to the <code>onclick</code> property will cause the <strong><a href="https://developer.mozilla.org/en/DOM/element.onclick" rel="nofollow noreferrer"><code>onclick</code></a></strong> event handler to be defined.</li> <li>Invokes the <strong><a href="http://code.google.com/apis/youtube/js_api_reference.html#loadVideoById" rel="nofollow noreferrer"><code>loadVideoById</code></a></strong> method of the <code>player</code> object (as defined by the <a href="http://code.google.com/apis/youtube/js_api_reference.html" rel="nofollow noreferrer">YouTube javascript API</a>).</li> <li>&nbsp;</li> <li>Invokes the function (created at lines 5-9), passing <code>link[1]</code> as a first parameter.</li> </ol> <h2>References</h2> <ul> <li><strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/for" rel="nofollow noreferrer"><code>for</code></a></strong> loops and the <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/continue" rel="nofollow noreferrer"><code>continue</code></a></strong> statement</li> <li>Creating and calling <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Statements/function" rel="nofollow noreferrer"><code>functions</code></a></strong> in JavaScript</li> <li><strong><a href="https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions" rel="nofollow noreferrer">Regular Expressions</a></strong> (RegExp).</li> <li><strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/test" rel="nofollow noreferrer"><code>test</code></a></strong> method of the <code>RegExp</code> object</li> <li>The <strong><a href="https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/match" rel="nofollow noreferrer"><code>match</code></a></strong> function in conjunction with a Regular expression</li> <li>The <strong><a href="https://developer.mozilla.org/en/DOM/element.innerHTML" rel="nofollow noreferrer"><code>innerHTML</code></a></strong> property of an element</li> <li>The <strong><a href="https://developer.mozilla.org/en/DOM/element.onclick" rel="nofollow noreferrer"><code>onclick</code></a></strong> event handler</li> <li>The <strong><a href="http://code.google.com/apis/youtube/js_api_reference.html#loadVideoById" rel="nofollow noreferrer"><code>loadVideoById</code></a></strong> method of the <strong><a href="http://code.google.com/apis/youtube/js_api_reference.html" rel="nofollow noreferrer">YouTube JavaScript API</a></strong> </li> </ul> <h2>Another interesting answer</h2> <ul> <li><a href="https://stackoverflow.com/questions/7443578/youtube-iframe-api-how-do-i-control-a-iframe-player-thats-already-in-the-html/7513356#7513356">YouTube iframe API: how do I control a iframe player that&#39;s already in the HTML?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload