www.webdeveloper.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: Ajax doesn't load other javascripts

  1. #1
    Join Date
    Aug 2010
    Posts
    25

    Ajax doesn't load other javascripts

    I have an ajaxfunction that fetches information from a server. It calls a <div> where there's some PHP and PHP-functions and there are other javascript functions inside but they don't work when I call the Ajax function.

    I get the feeling the ajax misses the definitions of the javascript functions (not the call to the functions) as the definitions are not included in the PHP, but when I wrote a <HTML><HEAD> with the javascript function definitions inside it didn't work anyway.

    How do I solve this?

  2. #2
    Join Date
    Mar 2010
    Posts
    2,803
    I have no idea without seeing your code.

    For starters, what is the "it" that is calling a <div> in

    It calls a <div>.......
    I skipped school the day they taught mind reading.

  3. #3
    Join Date
    Aug 2010
    Posts
    25
    This javascript line in the main index page calls the function...

    xmlhttp.open("GET","phpfunctions.php?sortmc="+sortering+"&categorymc="+kategori+"&page="+page+"&sele ct="+selected,true);

    In my main index page I have this line with the div-tag which is called by the ajax-function:
    <div id="replace"><b><?php include "phpfunctions.php" ?></b></div>

    In phpfunctions.php I have a call to Wimpybutton:

    <script language="JavaScript" type="text/JavaScript">writeWimpyButton("'.$row['producturl'] . '", "20", "20", "&PlayingColor=00FFFF");</script>

    But this button is not shown when the ajaxfunction is called.

  4. #4
    Join Date
    Mar 2010
    Posts
    2,803
    You're only posting bits and pieces of your code instead of all of it.

    The problem might be in the writeWimpyButton function or it could be somewhere else in your code. Without seeing all your code I can't tell. Hopefully someone will come along to help you.

  5. #5
    Join Date
    Aug 2010
    Posts
    25
    Thanks, but I don't think the entire code is needed. It's all about the ajax missing the javascript code and that is the issue that has to be solved. Here's from another forum:

    "when you 'load' JavaScript-code with ajax then you have to eval() it explicitly since this is not similar to a 'normal' page-load and the code isn't evaled and you cannot use it."

    So from a starting point it's more about getting the ajax to evalute the javascript code that is inside the PHP.

  6. #6
    Join Date
    Jan 2009
    Posts
    3,346
    Scripts are not automatically parsed when using innerHTML (though event based javascript will work just fine such as inline onclick attributes). You can loop through to find the <script> tags and eval them if you wish. This may work but poses a potential security issue and this is usually evidence of the need for a slight re-architecture of the solution.

  7. #7
    Join Date
    Aug 2010
    Posts
    25
    Ok, well I need the ajax since it speeds up the page and within I also need the player button so I can't see how to reorganize it.

    What security issue are you talking about? And how do I eval the javascript? (I'm a total newbie at javascript).

    eval("<script src="http://www.mywebpage.com/pages/wimpy_button.js" type="text/javascript"></script>"); ??

    It doesn't seem to work. *lol* Like I said, total newbie.

  8. #8
    Join Date
    Jan 2009
    Posts
    3,346
    What kind of "player button"? As I said before appended onclick attributes work just fine when calling functions that have already been loaded. What exactly does your "player button" do?

  9. #9
    Join Date
    Aug 2010
    Posts
    25
    The player button (Wimpybutton) plays an MP3 music file.

    writeWimpyButton("'. $row['producturl'] . '", "20", "20", "&PlayingColor=00FFFF");

    First variable is URL to musicfile, 20 and 20 is size of player button and the last variable is color.

  10. #10
    Join Date
    Jan 2009
    Posts
    3,346
    Perhaps a JSON version would be more appropriate then? Your PHP could send back some contents and the $row['producturl'] and your javascript can execute the function call. Is that the only thing returned by the AJAX call?

  11. #11
    Join Date
    Aug 2010
    Posts
    25
    This is what's being echoed back (1 row): title, artist, category, wimpy player button, buybutton. (There are 20 rows on a page. )

    The code:

    echo '<tr>';
    echo '<td width="200" class="datacellnopic">' . $row['title'] . '</td>';
    echo '<td width="200" class="datacellnopic"><b>' . $row['artist'] . '</b></td>';
    echo '<td width="100" class="datacellnopic" style="font-size:10px font:Arial, Helvetica, sans-serif">' . $row['category'] . '</td>';
    echo '<td width="30" align="center" class="datacellnopic">';
    echo '<script language="JavaScript" type="text/JavaScript">
    writeWimpyButton("'. $row['producturl'] . '", "20", "20", "&PlayingColor=00FFFF");</script>
    </td>
    <td align="center" class="datacellnopic">

    <a href="http://www.mypage.com/pages/popup.php?artist=' . stripslashes($row['artist']) . '&presurl=' . $row['presurl'] . '&title=' . stripslashes($row['title']) . '&product=' . $row['product'] . '&producturl=' . $row['producturl'] . '&price=' . $row['price'] . '&code=' . $row['code'] .'" rel="gb_page_center[500, 500]">
    <img src="../images/buybutton.gif" border=0 width="42" height="24"></a>
    </td>
    </tr>
    ';

    Perhaps it should be mentioned that the CSS-popup done in javascript (rel="gb_page_center[500, 500]") stopped working as well when ajax was implemented.

  12. #12
    Join Date
    Jan 2009
    Posts
    3,346
    What does writeWimpyButton do? Does it make use of document.write()?

  13. #13
    Join Date
    Aug 2010
    Posts
    25
    Below is the entire wimpy_button.js. At the very bottom of the function writeWimpyButton() (at the bottom of the page) there are two lines of document.write() but one of them is commented behind //.

    ///////////////////////////////////////
    // //
    // Wimpy Button Maker //
    // v 4.1.5 //
    // 2009 Plaino //
    // Available at //
    // www.wimpyplayer.com //
    // //
    ///////////////////////////////////////

    // OPTIONS:
    // When a button is clicked, the other buttons will stop playing.
    // This option sets the state that the other buttons will go into.

    // The following (default) will put the other buttons into the "pause" state.
    // If the button is clicked again, the track will continue where it left off.
    // var wimpyButtonRewindOrPause = "pause";

    // The following will put the other buttons will stop and rewind.
    // If the button is clicked again, the track will start from the beginning.
    var wimpyButtonRewindOrPause = "pause";


    var wimpyUserAgent = navigator.appName.indexOf("Microsoft");
    var wimpyButtonIDs = wimpyButtonIDs || Array();
    function wimpyButtonStopOthers(myid_in){
    for(i=0; i<wimpyButtonIDs.length; i++){
    if(wimpyButtonIDs[i][0] != myid_in){
    if (wimpyUserAgent != -1) {
    if(wimpyButtonRewindOrPause == "pause"){
    window[wimpyButtonIDs[i][0]].js_wimpy_pause();
    } else {
    window[wimpyButtonIDs[i][0]].js_wimpy_stop();
    }
    } else {
    if(wimpyButtonRewindOrPause == "pause"){
    document[wimpyButtonIDs[i][0]].js_wimpy_pause();
    } else {
    document[wimpyButtonIDs[i][0]].js_wimpy_stop();
    }
    }
    }
    }
    }
    function wimpyButtonTrackStarted(fileName){
    var len = wimpyButtonIDs.length;
    for(i=0; i<len; i++){
    if(unescape(wimpyButtonIDs[i][1]) == unescape(fileName)){
    wimpyButtonStopOthers(wimpyButtonIDs[i][0]);
    break;
    }
    }
    }
    function writeWimpyButton(theFile, wimpyWidth, wimpyHeight, wimpyConfigs, backgroundColor){
    var wimpyReg = "REGISTRATION_CODE_HERE";
    var defaultWidth = 35;
    var defaultHeight = 35;
    var defaultConfigs = "";
    var baseURL = "";
    var wimpySwf = "wimpy_button.swf";
    var wimpyWidth = (wimpyWidth == null) ? defaultWidth : wimpyWidth;
    var wimpyHeight = (wimpyHeight == null) ? defaultHeight : wimpyHeight;
    var wimpyConfigs = (wimpyConfigs == null) ? defaultConfigs : wimpyConfigs;
    var backgroundColor = (backgroundColor == null) ? false : backgroundColor;
    var myid = "wimpybutton"+Math.round((Math.random()*1000)+1);
    var myInfo = Array(myid, baseURL+theFile);
    wimpyButtonIDs[wimpyButtonIDs.length] = myInfo;
    var flashCode = "";
    var newlineChar = "\n";
    var backgroundColor = (backgroundColor == null) ? false : backgroundColor;
    if(typeof(backgroundColor) == "string"){
    var Astring = backgroundColor.split("");
    if(Astring[0] == "#"){
    Astring.shift();
    backgroundColor = Astring.join("");
    }
    }
    if(backgroundColor == false){
    tptParam = '<param name="wmode" value="transparent" />'+newlineChar;
    tptEmbed = ' wmode="transparent"';
    } else {
    tptParam = '<param name="bgcolor" value="#'+backgroundColor+'" />'+newlineChar;
    tptEmbed = ' bgcolor="#'+backgroundColor+'"';
    }
    flashCode += '<object classid="clsid27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+wimpyWidth+'" height="'+wimpyHeight+'" id="'+myid+'">'+newlineChar;
    flashCode += '<param name="movie" value="'+wimpySwf+'" />'+newlineChar;
    flashCode += '<param name="loop" value="false" />'+newlineChar;
    flashCode += '<param name="menu" value="false" />'+newlineChar;
    flashCode += '<param name="quality" value="high" />'+newlineChar;
    flashCode += '<param name="wmode" value="transparent" />'+newlineChar;
    flashCode += '<param name="allowScriptAccess" value="always" />'+newlineChar;
    flashCode += '<param name="flashvars" value="theFile='+baseURL+theFile+wimpyConfigs+'&wimpyReg='+wimpyReg+'&myid='+myid+'" />'+newlineChar;
    flashCode += '<embed src="'+wimpySwf+'" width="'+wimpyWidth+'" height="'+wimpyHeight+'" flashvars="theFile='+baseURL+theFile+wimpyConfigs+'&wimpyReg='+wimpyReg+'&myid='+myid+'"'+tptEmbed+' loop="false" menu="false" quality="high" name="'+myid+'" align="middle" allowScriptAccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'+newlineChar;
    //document.write('<br>'+myid+'<br><textarea name="textarea" cols="40" rows="3">'+flashCode+'</textarea><br>')+newlineChar;
    document.write(flashCode);
    }

  14. #14
    Join Date
    Jan 2009
    Posts
    3,346
    Because you are planning to use document.write in that way you cannot just execute it from AJAX. You'll need to embed your flash in another way (may I recommend swfobject). Alternatives include splitting out the flash code using the PHP and returning the whole bit in the ajax (you can pass the needed variables from the javascript so the PHP will know what to put) or coming up with another way to embed the flash after placing the new content into your container.

  15. #15
    Join Date
    Aug 2010
    Posts
    25
    Ok, thanks a lot. Then the problem is the document.write()...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles