views:

95

answers:

3

It seems this is a known problem and has been asked several times before here in SO however I do not see anything specific to jQTouch so I thought I would give it a try.

jQT will dynamically load pages when a link is clicked. In this page I would like to include something like

<script>
$.include('javascriptfile.js', function() {alert('do something with results of this file to an already existing div element');};
</script>

The $.include is a jquery plugin I found that mimics the $.load with a few more smarts added to it. Tested to work on FF but not in Chrome or most importantly, Safari.

The alert is never displayed. FireBug never shows the javascript even being loaded. If I put an alert before the $.include I still do not see anything.

I have tried an onclick/ontap event that would then run this code that was included in the head tag, no luck.

Edit: I am using the r148 revision of jQT. This was working prior to moving to this version, i believe.

A: 

Did you try to add the javascript file using one of these two methods:

Static Way:

<script type="text/javascript">

function staticLoadScript(url){
   document.write('<script src="', url, '" type="text/JavaScript"><\/script>');
}

staticLoadScript("javascriptfile.js");
modifyDivFn(processFnInFile());

</script>

Dynamic way:

<script type="text/javascript">

function dhtmlLoadScript(url){
   var e = document.createElement("script");
   e.src = url;
   e.type="text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e);
}

onload = function(){
   dhtmlLoadScript("javascriptfile.js");
   modifyDivFn(processFnInFile());
}

</script>

After the include you can call a function that does the processing you want (that being processFnInFile()) which result will be passed to modifyDivFn (and modify the div you want.) You could do this in one function, just to illustrate the idea.


Source: Dynamically Loading Javascript Files

Geries
Ya, I tried having a static function in the head that would append to the HEAD. The problem, is that I can never call that static function. It is not an option to use the onload event, from what I know. The way jQT works is that you click on a link, in my case I am referencing html files retrieved through the internal GET mechanism that jQT uses, and then the contents of that file are placed in a dynamically generated div. So in this dynamically generated content, that html file, I have the script tag that would be calling your static function, but the code in this script tag is never executed
Luke Ollett
Let me see if I understood correctly, you click on a link, this one gets the content of a file and puts it into a div (which generates dynamically). So, when the content is put on that div, you want a script to be executed?
Geries
ya, that sounds about right.
Luke Ollett
then add a callback to the the load() event of the div that will contain the content that you load from the file. Checkout the documentation here:http://api.jquery.com/load-event/
Geries
doesnt a load event require a url/src element like img or a new page? I see what your saying and can understand your response since JQTouch wants me to put a html file in the href. I need ot research a bit how I can take over the default functionality since the JQT library is doing this load event you speak of.
Luke Ollett
A: 

Well Geries, I appreciate your help but ultimately the answer required a drastic rethinking of how I was using JQTouch. The solution was to move everything to an onclick event and make all the hrefs link to #. This might be what you were talking about Geries.

In the onclick function I do the logic, preloading, loading of the page through my own GET through jquery, then use the public object jQT.goTo(div, transition). This seems to get around the WebKit bugs or whatever I was running into and this now owrks on FireFox, Chrome, Safari, iPhone, and the lot.

I do run into a few animation issues with JQT but I think these are known issues that I hope Stark and the gang at JQTouch are working on.

Luke Ollett
A: 

Hi Luke,

I am also trying to do the same thing. Is it possible to show some example code for your solution please?

razorsniper