dcsimg


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: javascript within body in ajax

  1. #1
    Join Date
    Sep 2007
    Posts
    1

    javascript within body in ajax

    hi everybody,
    i am using ajax in my page to get contents and load them in a 'div' inside my main page. everything works fine as long as the content is a normal html code. but if the content contains javascript then it is not executing. e.g. the javascript of my content is
    Code:
    <script type="text/javascript">alert('content');</script>
    as i click a link to get this content, it is not executing.
    does anybody hav the answer for my problem?? thanx in advance...

  2. #2
    Join Date
    Sep 2007
    Posts
    3
    I have had the same problem but I found a workaround which is following. Note..it does not work in IE but works beautifully in Netscape and Firefox. May be you can tell me what is the problem with IE.

    1 - from your ajax script put a id on your script like.
    <script id="scriptFromAjax">
    2 - On Success from ajax add the following line.
    var response = o.responseText;
    document.getElementById("ajaxresponseDiv").innerHTML = response;
    //this is where you get all your script contents.
    var scriptEle = document.getElementById('scriptFromAjax')
    if(scriptEle!=null){
    LoadJS(scriptEle.innerHTML);
    }


    3- Paste the following function in your receiving page. Put a div tag with id=dinamicJScontainer.

    function LoadJS(theScript)
    {
    var newElem = document.createElement("script");
    var elemJS = document.getElementById("dinamicJScontainer");


    newElem.setAttribute("language","JavaScript1.2");
    newElem.setAttribute("type","text/javascript");
    //IE is failing on following line.
    newElem.innerHTML = theScript;
    alert(newElem.innerHTML );
    elemJS.appendChild(newElem);
    eliminar("dinamicJScontainer");

    };

    4- Optional -- call the following function if you want to clear the contents of jJavascript.
    function eliminar(x){
    while(document.getElementById(x).childNodes.length>0){
    document.getElementById(x).removeChild(document.getElementById(x).childNodes
    [0]);}
    };


    Hope this will help.
    Khalid Ansari

  3. #3
    Join Date
    Sep 2007
    Posts
    3
    JavaScript contents within AJAX response do work on IE. You need to get the <script node out of your ajax response and then eval() on it.

    In my case, I have done following.
    //response text is ajax response.
    var response = o.responseText;
    response = response.split("<!")[0];
    //I am getting first element (0) because I have script element first in my response.
    var b = response.split(/\<\/?script[^\<]*\>/i)[0];
    eval(b);
    document.getElementById(divid).innerHTML = response;

  4. #4
    Join Date
    Aug 2010
    Posts
    1
    Are you saying you got it working in IE? I am having this same problem, but don't understand what you did to make it work.. I'm getting a runtime error in IE

Similar Threads

  1. Javascript not working (ajax with asp)
    By Guitarkalle in forum AJAX
    Replies: 1
    Last Post: 06-13-2008, 10:35 PM
  2. USER BACKUP -What is wrong on the script?
    By kabucek in forum VB Classic
    Replies: 0
    Last Post: 10-04-2006, 10:49 AM
  3. Replies: 0
    Last Post: 08-28-2006, 04:05 AM
  4. New forum + some AJAX links
    By Lori in forum AJAX
    Replies: 0
    Last Post: 02-09-2006, 02:26 PM
  5. Sample Sites.
    By Murray Foxcroft in forum Web
    Replies: 5
    Last Post: 11-02-2000, 03:42 AM

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
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center