0

I'm using the the jQuery fileupload plugin and configure it like this:

jQuery(document).ready(function() {
  jQuery("#fileupload").fileupload({
    dataType: "json",
    url: "ajax_handler.php?globalVar=" + globalVar,
    send: function (e, data) {

    },
    done: function (e, data) {

    }
});

....

Where globalVar is (wait for it) a global variable.

The problem is that if I change the value of globalVar and then do a file upload (using the jQuery file upload plugin which is AJAX so that the page doesn't change), the URL that the request is made to has the original globalVar value (that it had when the page first loaded).

Why is this happening?

6
  • variable are global to context window and each page has its own context. Is it your issue? Commented Jan 4, 2014 at 17:24
  • How do you declare and set globalVar? Commented Jan 4, 2014 at 17:35
  • @A.Wolff It's echo'd out by PHP to the page. I was wondering if this problem might have something to do with the fact that the data being passed to fileupload() is an object (it is, right?). Like maybe the variables and functions inside the object are evaluated just once, when it's passed, instead of when the function is called? I also added a link to the plugin I'm using. Commented Jan 4, 2014 at 17:43
  • problem seems not related to plugin but how you set and update globalVar value. I still don't know how you are doing this?! Beware, php variable on page will be evaluated only once Commented Jan 4, 2014 at 17:58
  • 1
    you're setting the url property to a string when you create the file upload; whatever you do to globalVar after that won't affect the url property Commented Jan 4, 2014 at 18:00

2 Answers 2

2

When you create the file upload widget, you're passing it a configuration object. This object has a number of properties, among them url.

The property values are evaluated when the object is created (in your case when you create the file upload widget in $(document).ready()). The object has no knowledge of the globalVar variable since the value that is assigned:

"ajax_handler.php?globalVar=" + globalVar

evaluates to a simple string (you're not passing it a reference to globalVar or anything like that). The behavior you seem to be expecting could only happen if you'd assign a function to the url property in which you reference globalVar (I don't know whether the file upload plugin supports this).

So even if you change globalVar at a later time, the file upload widget's url configuration option will stay the same. If you want to change it, you need to explicitly assign it again.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, that makes perfect sense!
0

If you want a variable value to persist from one page to another, then you have to store that value somewhere and then retrieve it from the other page. Javascript variables are local to a page so the entire javascript state is cleared each time you go to a new page. The options for storing/retrieving the variable are:

  1. Store it and retrieve if from a cookie
  2. Store it and retrieve it from LocalStorage
  3. Store it and retrieve it on your server (probably using AJAX)

The first two are simpler if you don't need it stored on the server. The advantage of the server is that it can be available even from other computers. The cookie and LocalStorage are only available on that particular computer.

3 Comments

I'm not sure what you mean -- the page doesn't change? The file upload plugin is an AJAX function.
@Nate are you sure the page is not reloaded? Which exact plugin are you using?
@Nate - If the page URL is not changing at all, then perhaps your globalVar is not declared to be a global variable. If it is global, it will retain it's value for the lifetime of a particular page. So, either it's not global, a new page is being reloaded or some of your code is changing the value of the variable. Those are the options.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.