This discussion is archived
7 Replies Latest reply: Jun 2, 2010 8:25 AM by Yann39 RSS

Write Javascript from On Demand Process

Yann39 Journeyer
Currently Being Moderated
Hi,

I have the following Javascript/AJAX function :
function maj_arbre() {
     var get = new htmldb_Get(null,$x('pFlowId').value,'APPLICATION_PROCESS=Afficher_arbre',0);
     
     get.add('P2_FILTRE_ID',html_GetElement('P2_FILTRE_ID').value);

     gReturn = get.get();

     if(gReturn) {
          html_GetElement('P2_ARBRE').innerHTML = gReturn;
     }
     else {
          html_GetElement('P2_ARBRE').innerHTML = 'Erreur';
     }
     get = null;
}
who call an On Demand Process :
DECLARE
...
BEGIN
HTP.prn('<script type="text/javascript"> var ITEMS = [');
  for c in cur loop   
       ...
  end loop;
  HTP.prn(']; new arbre (ITEMS);</script>');
END;
This ODP returns the Javascript code to initialize a variable (ITEMS) and then call a function using it who should display some things.

But the javascript seems not executed, I can't find it in the page source code, I only see it using Firebug.

I made something wrong ?

Thanks.

Yann.

EDIT :

P2_ARBRE is a "Display As Text (does not save state)" item.

When I put the generated code directly on it (source expression) :
<script type="text/javascript">var ITEMS = [['0 Survey']];new tree (ITEMS, TPL);</script>
It shows nothing too.

Edited by: Yann39 on 1 juin 2010 03:13
  • 1. Re: Write Javascript from On Demand Process
    Yann39 Journeyer
    Currently Being Moderated
    It works when I put the generated code in a region source.
  • 2. Re: Write Javascript from On Demand Process
    Yann39 Journeyer
    Currently Being Moderated
    I can ask the question differently :

    Imagine I have a javascript variable in my page header :
    <script type="text/javascript">var myvariable;</script>

    A javascript function uses this variable in the body.
    <script type="text/javascript">myfonction(myvariable);</script>

    How can I dynamically change the content of this variable using my PL/SQL procedure ?

    Thanks.

    Yann.
  • 3. Re: Write Javascript from On Demand Process
    Yann39 Journeyer
    Currently Being Moderated
    More simple again :

    When I manually put the code in the P2_ARBRE item source, it works :
    <script type="text/javascript">
    var TREE_ITEMS = [["9431 Test1"],["10984 Test2"]];
    new tree(TREE_ITEMS);
    </script>
    When I call my application process to write exactly the same code in the item innerHTML, it fails, although the generated HTML code is right :
    <span class="display_only" id="P2_ARBRE">
    <script type="text/javascript">
    var TREE_ITEMS = [["9431 Test1"],["10984 Test2"]];
    new tree(TREE_ITEMS);
    </script>
    </span>
    so the problem is that this javascript code is not executed when set from an ODP.

    Why ?!

    [sorry for quadruple post !]
  • 4. Re: Write Javascript from On Demand Process
    Arie Geller Guru
    Currently Being Moderated
    Hello Yann,

    Changing the content of a DOM node using innerHTML doesn’t trigger any JavaScript functions or DOM events included in the new code. You should do it manually (the logic is to prevent endless loops in case of bad/malicious programming).

    If I understand your scenario correctly, you should try using the built-in JavaScript function eval() - http://www.w3schools.com/jsref/jsref_eval.asp . Change your ODP to return only the JavaScript code (without &lt;script> and stuff) and then use eval(gReturn).

    If you still need to set innerHTML, return the full string and then parse it in JavaScript, generating a proper string for the eval() call.

    General remark: using eval() considered to be a security hazard because it opens the door for JavaScript injection. If you choose to use it, you should scan the returned string (gReturn) for possible malicious code (for example the words ‘script’ should not be allowed, etc.).

    Regards,
    Arie.

    -------------------------------------------------------
    &diams; Please remember to mark appropriate posts as correct/helpful. For the long run, it will benefit us all.

    &diams; Author of Oracle Application Express 3.2 – The Essentials and More
  • 5. Re: Write Javascript from On Demand Process
    Yann39 Journeyer
    Currently Being Moderated
    Hi,

    OMG I forgot this eval() function, I'm so stupid...

    that's all I needed !

    Thank you Arie ;)
  • 6. Re: Write Javascript from On Demand Process
    Arie Geller Guru
    Currently Being Moderated
    Hello Yann,

    >> … I'm so stupid...

    No, you probably Not. As I said using eval() can be dangerous so it’s good to forget this function from time to time :)

    Regards,
    Arie.

    -------------------------------------------------------
    &diams; Please remember to mark appropriate posts as correct/helpful. For the long run, it will benefit us all.

    &diams; Author of Oracle Application Express 3.2 – The Essentials and More
  • 7. Re: Write Javascript from On Demand Process
    Yann39 Journeyer
    Currently Being Moderated
    Yes, but I am going to use this eval() function, for simplicity, as I don't have much time to spend on that, especially to parse the result from the ODP to rebuild the javascript table.

    So I will use the eval() function and parse the returned result to avoid dangerous code, anyway this is an internal application (intranet) on an HTTPS server, and the page require authentication, so I think security is already not so bad.

    My idea (before you remind me this eval() function) was to generate XML from the ODP, and then build the javascript table by parsing the XML. Maybe when the application will be ready and if I find time, I will improve that kind of things :)

    Thanks again.

    Yann.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points