ajax & php & javascript

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan P

    #1

    ajax & php & javascript

    Hello!

    I have a index.php that on one click calls via AJAX a file.php.

    index.php looks like this:

    <html >
    <head>

    <script language="javas cript" type="text/javascript" >


    var http; // We create the HTTP Object
    var dest;
    var prenosi;
    function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_vers ion >= 5)
    try {
    xmlhttp = new ActiveXObject(" Msxml2.XMLHTTP" );
    } catch (e) {
    try {
    xmlhttp = new ActiveXObject(" Microsoft.XMLHT TP");
    } catch (E) {
    xmlhttp = false;
    }
    }
    @else
    xmlhttp = false;
    @end @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
    xmlhttp = new XMLHttpRequest( );
    } catch (e) {
    xmlhttp = false;
    }
    }
    return xmlhttp;
    }

    function processStateCha nge(){
    if (http.readyStat e == 4){
    contentDiv = document.getEle mentById(dest);
    if ((http.status == 200)||(http.sta tus==0)){
    response = http.responseTe xt;
    contentDiv.inne rHTML = response;
    } else {
    contentDiv.inne rHTML = "Error: Status "+http.stat us;
    }
    }
    }



    function loadHTML(URL, destination){
    dest = destination;
    var url;
    http = getHTTPObject() ;
    if(http){
    http.onreadysta techange = processStateCha nge;
    http.open("GET" , URL, true);
    http.send(null) ;
    }
    }

    </head>
    <body>

    <span onclick="loadHT ML(file.php','' )">Click</span><br>
    </html>

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - -

    file.php looks like this:

    <span id="countdownco ntainer">
    write something down
    <script type="text/javascript">
    document.write( "1")
    </script>
    </span>

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    - - - - - - - - - -

    The problem is that "write something down" displays on the Internet
    Explorer, but number 1 doesn't. If we call just file.php - it works then -
    it displays both. Why is javascript not executing from ajax called
    procedure?

    Ivan Petrovic


  • Robin

    #2
    Re: ajax &amp; php &amp; javascript

    Ivan P wrote:[color=blue]
    >[snip]
    > <span onclick="loadHT ML(file.php','' )">Click</span><br>[/color]

    Is this really the code? Missing a ' before file.php and no dest specified?
    [color=blue]
    >[snip]
    > The problem is that "write something down" displays on the Internet
    > Explorer, but number 1 doesn't. If we call just file.php - it works then -
    > it displays both. Why is javascript not executing from ajax called
    > procedure?[/color]

    I'm not sure but perhaps when setting the innerHTML property the browser
    doesn't parse it for javascript?

    Robin

    Comment

    • ImOk

      #3
      Re: ajax &amp; php &amp; javascript

      The problem is that the javascript executes before the rendering is
      done of the page.

      Can you put an alert in its place to confirm this?

      Comment

      • the DtTvB

        #4
        Re: ajax &amp; php &amp; javascript

        > The problem is that "write something down" displays on the Internet[color=blue]
        > Explorer, but number 1 doesn't. If we call just file.php - it works then -
        > it displays both. Why is javascript not executing from ajax called
        > procedure?[/color]

        1. The document.write method can only be called while the page is
        loading, if we call it after the page was loaded or with any events,
        or with AJAX script, it will replace whole HTML data in IE, and
        in Firefox, it will do nothing.
        2. If the script tag is created dynamically by changing some
        element's innerHTML, it won't be executed.

        My way to create script on the fly is using this script
        scriptObj =
        document.body.a ppendChild(docu ment.createElem ent('script'));
        scriptObj.type = 'text/javascript';
        scriptObj.src = '<link to js file>';

        Comment

        Working...