简体   繁体   中英

jquery function - reassign global javascript variable

I am using an upload script that runs a function once the file is uploaded. I need this function to re-assign the value of a javascript variable. I need to re-assign "trackid" to the value that is inside my onComplete function. Can anyone help me out?

Edit: A little more explanation... I have 2 instances of this upload script on my page. I need to take the response from the first uploader and assign it as a URL parameter to the 2nd uploader. Does it have something to do with the document.ready? I will update my code.

1st script:

<script type="text/javascript">
trackid = 9999999;
</script>
<script type="text/javascript">
        jQuery(document).ready(function() { 

    $('#mainftp').uploadify({
    'uploader'  : 'js/uploadifyposted/uploadify.swf',
    'script'    : 'js/uploadifyposted/uploadify.php?<?php echo urlencode("privateFolderWav=" . $privateFolderWav . "&userid=" . $userid. "&songid=" . $songid);?>',
    'multi'         : true,
    'auto'          : true,
    'height'        :   '32', //height of your browse button file
    'width'         :   '250', //width of your browse button file
    'sizeLimit' :   '51200000',  //remove this to set no limit on upload size
    'simUploadLimit' : '3', //remove this to set no limit on simultaneous uploads
    'buttonImg' : 'img/browse.png',
    'cancelImg' : 'img/cancel.png',
        'folder'    : '<?php echo $privateFolderWav;?>', //folder to save uploads to
        onProgress: function() {
          $('#loader').show();
        },
        onComplete: function(event, queueID, fileObj, response, data, trackid) {
          $('#loader').hide();
          $('#allfiles').load(location.href+" #allfiles>*","");
          $('#filesUploaded').attr('value', ''+response+'');
          trackid = response;
          alert(trackid);

          //location.reload(); //uncomment this line if youw ant to refresh the whole page instead of just the #allfiles div
        }   
    });

    $('ul li:odd').addClass('odd');

}); 

</script>

2nd script:

<script type="text/javascript">


        jQuery(document).ready(function() { 

    $('#mainftp2').uploadify({
    'uploader'  : 'js/uploadifymultiple/uploadify.swf',
    'script'    : 'js/uploadifymultiple/uploadify.php?<?php echo urlencode("songid=" . $songid . "&userid=" . $userid . "&trackid=");?>'+trackid,
    'multi'         : true,
    'auto'          : true,
    'height'        :   '32', //height of your browse button file
    'width'         :   '250', //width of your browse button file
    'sizeLimit' :   '51200000',  //remove this to set no limit on upload size
    'simUploadLimit' : '3', //remove this to set no limit on simultaneous uploads
    'buttonImg' : 'img/browse.png',
    'cancelImg' : 'img/cancel.png',
        'folder'    : '<?php echo $multiFolder?>', //folder to save uploads to
        onProgress: function() {
          $('#loader2').show();
        },
        onComplete: function(event, queueID, fileObj, response, data) {
          $('#loader2').hide();
          $('#allfiles2').load(location.href+" #allfiles2>*","");
          $('#filesUploaded2').attr('value', ''+response+'');

          //location.reload(); //uncomment this line if youw ant to refresh the whole page instead of just the #allfiles div
        }   
    });

    $('ul li:odd').addClass('odd');

}); 

</script>

I'm not seeing a trigger in this code. It looks like both scripts run immediately once the page is loaded, if so, the second script isn't waiting for the variable reassignment. You should have that second function called in the onComplete of the first

Try setting:

trackid = 9999999;

instead of

var trackid = 9999999;

var foo = 'baa'; <- private variable foo = 'baa' <- global variable

For example:

`function print2() { var foo = 'baa'; }

print2(); alert(foo); `

don't print 'foo' is undefinied. ` function print2() { foo = 'baa'; }

print2(); alert(foo); ` print 'baa'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM