Hello,

I have this javascript function:

<script type="text/javascript">$(function() { YAHOO.MediaPlayer.addTracks(document.getElementById("ajazload"),1,false)});</scr*​ipt> <script type="text/javascript">

This function is loaded inside the <div id="main">, which is loaded via Ajax.

How can I prevent the javascript function to reload itself at every #main ajax page load ? I just want the javascript function loads at the first ajax load of the #main part of the page.

In fact, I want the Yahoo Media Player add tracks at the first load of the page, and not at every page load, because it's adding the same tracks at every page load...

Thanks !

Dani AI

Generated

Short, practical fix: stop adding every link the player finds by filtering the AJAX fragment first and only handing YAHOO.MediaPlayer the links that haven’t been added yet. That keeps the play buttons in the page while preventing duplicate tracks.

As hinted, a single global flag will only let the player run once ever; cookies persist across full page reloads (as discovered). Instead keep a page-memory set of seen track URLs and, on each AJAX insert, build a temporary container containing only new links and pass that to the player.

// run this from your AJAX success callback after you insert the #main fragment
// call addUniqueTracksFrom($('#main')) or adjust selector to your fragment

(function(){
  // per-page memory (works until the user reloads the page)
  window._YMP_seen = window._YMP_seen || (typeof Set !== 'undefined' ? new Set() : {});

  function seenHas(href){
    return (window._YMP_seen instanceof Set) ? window._YMP_seen.has(href) : (window._YMP_seen[href] !== undefined);
  }
  function seenAdd(href){
    if (window._YMP_seen instanceof Set) window._YMP_seen.add(href);
    else window._YMP_seen[href] = 1;
  }

  function addUniqueTracksFrom($container){
    var $links = $container.find('a[href$=".mp3"]');
    if (!$links.length) return;
    var $temp = $('<div>');
    $links.each(function(){
      var href = this.href;               // normalized absolute URL
      href = href.split('?')[0];         // optional: strip querystrings for dedupe
      if (!seenHas(href)){
        seenAdd(href);
        $temp.append($(this).clone());
      }
    });
    if ($temp.children().length) {
      YAHOO.MediaPlayer.addTracks($temp.get(0), 1, false);
    }
  }

  window.addUniqueTracksFrom = addUniqueTracksFrom; // expose for your AJAX hook
})();

Notes and pitfalls:

  • Normalize URLs (use the element’s href) so relative vs absolute URLs don’t fake duplicates.
  • If tracking by URL isn’t enough (query strings, CDNs), use a canonical attribute like data-track-id.
  • This approach stores seen tracks only in memory (per-page). If you need a full reset, clear window._YMP_seen.
  • If YMP exposes a playlist API you can also compare against the player’s current list instead of maintaining your own set.

Recommended Answers

All 2 Replies

This might make it work once only:

<script type="text/javascript">
if (yahooLoaded === undefined) {
  var yahooLoaded = 0;
}
if (yahooLoaded != 1) {
  YAHOO.MediaPlayer.addTracks(document.getElementById("ajazload"),1,false);
  yahooLoaded = 1; 
}
</scr​ipt> <script type="text/javascript">

But it may may it work once EVER (until the JS environment is reinitialized).

More context would help to provide a better solution.

Thank you very much for your help.

The code broke my ajax loading. However, I found a solution via cookies with Jquery, but I realized that my idea was wrong, because as you said, it loads once ever, so tracks are not playable after one reload (and I want to keep the button play next to every mp3 link).

Here is my test site, that is more talking that thousand words (and I'm not english well speaker...):
http://www.actupost.com

In fact, I'd like the Yahoo Media Player, in the left bottom, not add duplicate tracks at every load. However, I'd like to keep the play buttons on pages.

To make tests, just click several times on the first post and you'll see the playlist rise at every load. May you have solution to not make the playlist rise indefinitely with duplicate tracks ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.