Magento Expert Forum - Improve your Magento experience

Results 1 to 3 of 3

How to compile Javascript loaded by Ajax?

  1. #1
    Junior Member Michelle_Magestore's Avatar
    Join Date
    Jun 2013
    Posts
    47
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default How to compile Javascript loaded by Ajax?

    Hi everyone. We know that Javascript is compiled when the browser loads your webpage. However, sometimes you need to compile your script loaded by Ajax, how should you do? As default, the browser doesn’t compile the script responded by Ajax. If you use the function eval() to compile your script, the variable (defined in your Ajax response code) will be unavailable to call in your page. Thus I have a tip that may help you solve this issue. You can call the function below to compile your script loaded by Ajax.

    // Compile your custom script
    function compileJsAjax(yourScript){
    var jsElement = document.createElement('script');
    jsElement.type = 'text/javascript';
    jsElement.text = yourScript;
    var existedJs = document.getElementsByTagName('script')[0];
    existedJs.parentNode.insertBefore(jsElement,existe dJs);
    }
    For example:

    new Ajax.Request(requestUrl,{
    method: 'post',
    postBody: '',
    parameters: '',
    onComplete: function(xhr){
    var response = xhr.responseText;
    var scripts = response.extractScripts();
    for (var i=0;i<scripts.length;i++)
    compileJsAjax(scripts[i]);
    }
    });
    If your response is a Javascript source (such as: http://yourstore.com/test.js), you can modify the function to get and compile this source.

    // Load and compile your script source
    function ajaxLoadJs(jsSource){
    var jsElement = document.createElement('script');
    jsElement.type = 'text/javascript';
    jsElement.async = true;
    jsElement.src = jsSource;
    var existedJs = document.getElementsByTagName('script')[0];
    existedJs.parentNode.insertBefore(jsElement,existe dJs);
    }
    I hope this helps. Thanks for reading! For other informative articles, visit our Magento blog!

    Anyone wants $100 to purchase any extensions? JOIN NOW

    View more threads in the same category:


  2. The Following User Says Thank You to Michelle_Magestore For This Useful Post:

    blanka (22-03-2014)

  3. #2
    New member
    Join Date
    Feb 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I keep getting a message that I need to enable Javascript. Javascript is enabled. I have checked everything I possibly can. I have turned off my firewall and anti virus and still no luck. I have reinstalled the IE and nothing. Any help would be great.
    ________________
    easy recipes for dinner ~ healthy dinner recipes ~ simple recipes for dinner

  4. #3
    New member blanka's Avatar
    Join Date
    Apr 2013
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Thank Michelle. This post is very useful for me!

Bookmarks

Posting Permissions

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