Rob
Rob

Reputation: 235

obtain a js variable from an ajaxed page

My question is this, if I have a page say index.HTML that has some script in, something simple like...

<script type="text/JavaScript">
$(document).ready(function() {
    Var buttonBox = {};
})
</script>

Obviously there would need to be more, I'm trying to make this simple. Then I use ajax to retrieve some data from the db and fill in the contents of a div, but in my return page I have another script tag, something like...

<script type="text/JavaScript">
$(function() {
    buttonBox.start = "some variable or string";
})
</script>

Along with the HTML content. Why is buttonBox.start not available in the main index.HTML page? Is there a way to make it available? Is formatting the output of my server page as a huge json object then parsing through it to set every needed variable along with the HTML content the best/only way to achieve this?

Thank you for the help, if you need more info I'll be happy to provide it, I was just minifying this for sake of ease.

Upvotes: 0

Views: 75

Answers (2)

panda
panda

Reputation: 1344

for each function in each tag, if the function is not global, it can not be reused. to make a function become global, you should use window. Also, you can put this function into an external file and load with

Upvotes: 0

albertjan
albertjan

Reputation: 7817

you could add the buttonBox to the window and make it global:

$(document).ready(function() {
     window.buttonBox = {};
});

$(function() {
    window.buttonBox.start = "some variable or string";
});

Upvotes: 4

Related Questions