If you want JavaScript code to execute when fragments are loaded into
a framework such as iUI here is how we do it.

First, make sure you are using the "latest" (at least v 0.40-dev1)
version of iUI which has dispatchEvent hooks built into it.

Next create a little javascript file for your custom use in your
project (say it is myproject.js).

Here is an extracted snippet from our custom project file which
contains just the code required to do the "script execution"

<CODE>  /* This would be your myproject.js */

/* Special scripts for the Mediweb iPhone project
 * wrh Nov 2009  - SVMHS
 * St. Vincents and Mater Health Sydney
 */

(function () {
addEventListener("load", function(event)
{
document.body.addEventListener('beforeinsert', beforeFragmentInsert,
false);
}, false);
})(); // End of the self-executing function that loads the event
handlers


//This is the event handler for "beforeinsert" event in iUI
function beforeFragmentInsert(event) {

// if we find any inline scripts - execute them
var f = event['fragment'];
if (f.tagName.toLowerCase() == "script") // check the root of the
fragment
        eval(f.innerHTML);

var n = f.getElementsByTagName("script"); // and any elements below
the root
for (z=0; n && z<n.length;z++)
        {
        eval(n[z].innerHTML);
        f.removeChild(n[z]); // prevent the script from flashing on
the screen
        }
}


/* This would be the end of the myproject.js snippet */</CODE>



Now in the <head> element of the first HTML page of your iUI project
add the following line as the last js declaration:

<script type=\"application/javascript\" src=\"myproject.js\"></script>


Now you will find that any <script> elements contained in the fragment
will execute after your fragment has been downloaded by Ajax and
before it is actually inserted into the displayable document.  If you
want it after the insert into the document there would be a couple of
small changes required.

Good luck,
Skip


















/* Special scripts for the Mediweb iPhone project
 * wrh Nov 2009  - SVMHS
 * St. Vincents and Mater Health Sydney
 */

(function () {
addEventListener("load", function(event)
{
document.body.addEventListener('beforeinsert', beforeFragmentInsert,
false);
}, false);
})(); // End of the self-executing function that loads the event
handlers


//This is the event handler for "beforeinsert" event in iUI
function beforeFragmentInsert(event) {

// if we find any inline scripts - execute them
var f = event['fragment'];
if (f.tagName.toLowerCase() == "script") // check the root of the
fragment
        eval(f.innerHTML);

var n = f.getElementsByTagName("script"); // and any elements below
the root
for (z=0; n && z<n.length;z++)
        {
        eval(n[z].innerHTML);
        f.removeChild(n[z]); // prevent the script from flashing on
the screen
        }



On Dec 4, 1:14 am, Bontas George <[email protected]> wrote:
> Sorry skip!!! I has not attentive and did not knew about this issue!! I'll be 
> more attentive regarding hijacking!!! :p
>
> But if you can help I badly need it!!! this fragments are really killing me 
> slowly.
>
> 10x and sorry again,
>
> George
>
> ________________________________
> From: skip <[email protected]>
> To: iPhoneWebDev <[email protected]>
> Sent: Thu, December 3, 2009 11:28:36 AM
> Subject: Re: javascript execution
>
> This thread was initially about iPhone screen resizing during drop
> down selects.  You have hijacked the thread to discuss execution of
> Javascript in fragments, and also changed the title of the thread.  At
> the very least this is unhelpful.  If you promise not to hijack
> anymore discussion threads I will tell you how to make javascripts
> execute when fragments are loaded - we do it all the time.
>
> You haven't helped me to solve my problem at all.
>
> Skip
>
> On Dec 3, 1:01 am, Bontas George <[email protected]> wrote:
>
>
>
> > Hello everybody,
>
> > I have another question that keeps me busy!!! I need on loading of some 
> > pages to run a javascript! how can this be done!
> >  example: the javascript is not executed!!! :((
> > <div id="graph" title="Graph" >
> >    <fieldset>
> >         <div class="row">
>
> >             <table style="width:290px;height:20px; background-color:#FFF">
> >             <tr>
> >                 <td align="center" bgcolor="#2d3642" style="color:#FFF">50 
> > min</td>
> >                 <td align="center" bgcolor="#2d3642" style="color:#FFF">75 
> > min</td>
> >                 <td align="center" bgcolor="#2d3642" style="color:#FFF">100 
> > min</td>
> >             </tr>
> >             </table>
>
> >             <div id="placeholder" style="width:290px;height:150px; 
> > background-color:#000"></div>
>
> >         </div>
> >         <div class="row">
> >         <p>
> >         <div class="whiteButton"  onclick="graph()">View graph</div>
> >         </p>
> >         </div>
> >     </fieldset>
> > </div>
>
> > <script type="text/javascript">
> > $(document).ready(function(){
> >     var d1 = [];
> >     for (var i = 0; i < 14; i += 0.5)
> >         d1.push([i, Math.sin(i)]);
>
> >     var d2 = [[0, 3], [4, 8], [8, 5], [9, 13]];
>
> >     // a null signifies separate line segments
> >     var d3 = [[0, 12], [7, 12], null, [7, 2.5], [12, 2.5]];
>
> >     $.plot($("#placeholder"), [ d1, d2, d3 ]);
>
> > });
>
> --
>
> You received this message because you are subscribed to the Google Groups 
> "iPhoneWebDev" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/iphonewebdev?hl=en.

--

You received this message because you are subscribed to the Google Groups 
"iPhoneWebDev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/iphonewebdev?hl=en.


Reply via email to