javascript - Pass a global variable to the $(document).ready function in jQuery - Stack Overflow

I have a site that has particular alphanumeric ID for every song, say like 57bfab618de4191 In the jQuer

I have a site that has particular alphanumeric ID for every song, say like 57bfab618de4191 In the jQuery player the MP3 song has to be assigned via link.

The jplayer function is this:

     $(document).ready(function (){
   function playAudio(val) {newVal=val}    

        $("#jquery_jplayer_1").jPlayer({
            ready: function (event) {
                $(this).jPlayer("setMedia", {
    mp3:".php?file="+newVal                       });
            },
            swfPath: "js",
            supplied: "mp3",
            wmode: "window"
        });
    });

I want to play different song present in the list for user so I passed a ID of song via button onClick like this

  onClick="playAudio(<?php echo "'".$songid. "'"; ?>)"  

Where song ID is the iD of the song specified in database, like 57bfab618de4191

This way I can pass the value, but I am not able to transfer the parameter of playAudio function to the document.ready function.

I have a site that has particular alphanumeric ID for every song, say like 57bfab618de4191 In the jQuery player the MP3 song has to be assigned via link.

The jplayer function is this:

     $(document).ready(function (){
   function playAudio(val) {newVal=val}    

        $("#jquery_jplayer_1").jPlayer({
            ready: function (event) {
                $(this).jPlayer("setMedia", {
    mp3:"http://website./dlb.php?file="+newVal                       });
            },
            swfPath: "js",
            supplied: "mp3",
            wmode: "window"
        });
    });

I want to play different song present in the list for user so I passed a ID of song via button onClick like this

  onClick="playAudio(<?php echo "'".$songid. "'"; ?>)"  

Where song ID is the iD of the song specified in database, like 57bfab618de4191

This way I can pass the value, but I am not able to transfer the parameter of playAudio function to the document.ready function.

Share Improve this question edited Oct 8, 2012 at 8:02 jonsca 10.4k26 gold badges56 silver badges63 bronze badges asked Sep 9, 2012 at 6:57 Abhishek SharmaAbhishek Sharma 3,3401 gold badge22 silver badges38 bronze badges 4
  • Echo the ID into a data attribute and get it with jQuery inside the function, and get rid of that inline click handler. – adeneo Commented Sep 9, 2012 at 7:00
  • @adeneo how,, actually, can you describe? I don't want page to be refreshed and also , the id is present at the play button of the song and there is list i cant assign the id globally anywhere. I have to use onClick function for specific song – Abhishek Sharma Commented Sep 9, 2012 at 7:04
  • I'm guessing something like THIS FIDDLE... – adeneo Commented Sep 9, 2012 at 7:09
  • try mp3:"http://website./dlb.php?file="+newVal+".mp3" – Alfred Commented Sep 9, 2012 at 7:09
Add a ment  | 

2 Answers 2

Reset to default 3
var val = 'whateveer'

function playAudio(nval) {
    var val = nval;
    $("#jquery_jplayer_1").jPlayer({
        ready: function(event) {
            $(this).jPlayer("setMedia", {
                mp3: "http://website./dlb.php?file=" + val
            });
        },
        swfPath: "js",
        supplied: "mp3",
        wmode: "window"
    });
}

no need for $(document).ready


if you need to change songs:

var val = '12345'; // your first song
$(document).ready(function() {
    $("#jquery_jplayer_1").jPlayer({
        ready: function(event) {
            $(this).jPlayer("setMedia", {
                mp3: "http://website./dlb.php?file=" + val
            });
        },
        swfPath: "js",
        supplied: "mp3",
        wmode: "window"
    });
});

function playAudio(nval) {
    var val = nval;
    $("#jquery_jplayer_1").jPlayer({
        "setMedia", {
            mp3: "http://website./dlb.php?file=" + val
        }
    });
    // --- OR ---
    $("#jquery_jplayer_1").changeAndPlay("http://website./dlb.php?file=" + val);
}

More info:

  • Changing songs on jPlayer by clicking a link, hosted on Amazon S3
  • http://www.jplayer/0.2.1/developer-guide/#jPlayer-change

You need to play with variable scoping.

var newVal = 'default_song_to_play_if_any';

function playAudio(val){
    newVal = val;
    $("#jquery_jplayer_1").jPlayer("destroy");
    $("#jquery_jplayer_1").jPlayer({
       ready: function (event) {
          $(this).jPlayer("setMedia", {
             mp3:"http://website./dlb.php?file="+newVal
          });
       },
       swfPath: "js",
       supplied: "mp3",
       wmode: "window"
   });
}

//use this ready function only if you want to play a default song on load
$(document).ready(function(){
    playAudio(newVal); 
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744771391a4592780.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信