Issachar Brooks wrote:
> Just to complement, Scripts that are triggered by a click etc inside 
> the Ajax requst do work. It is just the "inline" scripts that do not run.

This is the difference between executing scripts, and loading scripts. 
As you have discovered, code that can run right now, in the current 
moment in the current context, runs fairly easily. The problem comes 
when you try to load a function that will be called later. The problem 
is you have to actually store it somewhere. You have to add it to the 
DOM in a way that renders it executable.

I had to deal with this a while back. I came up with the code below. As 
a bonus, I am also including a similar function that loads a stylesheet 
on demand.

In my application I issue an ajax call. In the response I include 
content to be displayed and javascript to react to that content. To make 
it easier, I return it in a two element array like this: 
[script:{[name:<scrId>],[code:<scrCode>]},content:<content>]

It isn't completely easy, but it does work.

BTW, I am just a hacker like the rest of us who happens to have been 
down this road already. If any one has any better ideas, I would love to 
hear them.

-----------------------------------code-----------------------------------
    /* Load a script into the DOM
     scrId:   String: an identifier for this script object.
              (used to check if it is already loaded.)
     scrCode: String: The actual javascript code to load.
              (Just the code, do not include the script tag.)
  */           
  function loadScript(scrId, scrCode) {
    if(document.getElementById(scrId) == null) {
      var scrHandle = document.createElement("script");
      scrHandle.id = scrId;
      scrHandle.type = 'text/javascript';
      if (scrHandle){
        document.getElementsByTagName("head").item(0).appendChild(scrHandle)
            scrHandle.text = scrCode;
      }
    }
  }

/* Checks to see if a style sheet is currently found in the DOM
     href: String: the addressof the stylesheet file to check.
  */           
  function getStyleSheet(href) {
    return 
$A(document.getElementsByTagName("head").item(0).getElementsByTagName('link')).find(
 
function(s) {
        return (s.href.indexOf(href) > 0);
    });
  }

  /* Load a new style sheet into the DOM
     href: String: the addressof the stylesheet file to load.
           (also used to check if it is already loaded.)
  */           
  function loadStyleSheet(href) {
    if(!getStyleSheet(href)) {
      var newlink = document.createElement("link");
      newlink.setAttribute("rel",  "stylesheet");
      newlink.setAttribute("type", "text/css");
      newlink.setAttribute("href", this_pageroot+href);
      if (newlink){
        document.getElementsByTagName("head").item(0).appendChild(newlink)
      }
    }
  }
      



_______________________________________________
Javawin mailing list
[email protected]
http://mail.xilinus.com/mailman/listinfo/javawin_xilinus.com

Reply via email to