0

I am working on a code where I am genrating my own divs using mustache and appending it to the main index.html (keeping this a single page application).

I know if I use jQueryMobile, there is a page event 'beforePageShow' which is fired just before the page starts loading. You can use this to set some dynamic variables that is used in the page.

I want to do something same in my page where I am rendering a div: a new one if its not already appeneded to index.html or just hide/shiow if it already exists. Although I want to initialize some environment variables before the div loads. Is this possible?

Please help.

Thanks. Ankur

eightShirt
  • 1,457
  • 2
  • 15
  • 29
curioussam
  • 439
  • 3
  • 9
  • 19
  • possible duplicate of [Detect changes in the DOM](http://stackoverflow.com/questions/3219758/detect-changes-in-the-dom) – Barmar Sep 19 '13 at 23:46

3 Answers3

0

Use the document.ready method.

Visit here to see all methods you need: http://www.w3schools.com/jquery/default.asp

  • but I want that on demand. Consider this case: Somewhere in the application a link is pressed. Now what happens is that a functions is called, say showDiv that takes in some data and renders the template that will be displayed as the DIV. What I want is that before this DIV is rendered, some function is called that sets the variables in the div or something – curioussam Sep 19 '13 at 23:40
0

Instead of using the jQuery document on load you should put the variable directly on to the page, so instead of doing this

<script>
$(document).ready(function() {
   //this is the environment variable you want to initialise
   var variable = 'variable';
});
</script>

you would do this :

<script>
//this is the environment variable you want to initialise
var variable = 'variable'; 
</script>

Also remember to put this in the head section.

Yussuf S
  • 2,022
  • 1
  • 14
  • 12
0

I don't really understand why you would want to do something before the DOM is ready...

You have your HTML... that you know because you coded it. Then, if you want to generate new divs on the fly with jQuery... Why not, but still, why ? If you know you have to add some div, what makes it necessary to wait client-runtime ? If it's before DOM is ready, then your server should be able to send the appropriate data right away... If it's after the DOM is ready, then you have jQuery(document.ready) event.

Adrien Gorrell
  • 1,291
  • 3
  • 14
  • 28