Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Jun 2009
    Posts
    62
    Thanks
    9
    Thanked 0 Times in 0 Posts

    <script> tags returned by AJAX not working

    Hey all,

    I have some AJAX on my site, and part of what it returns is a piece of code that is in the <script> tags, but this is totally ignored (I'm assuming becuase of <scritp> tags within the original set of <script> tags.

    Does anybody know a way around this?

    Example code -

    Code:
    <script type="text/javascript">
        ...Code before
    
        document.getElementById('service-content').innerHTML = response;
    
        Code after....
    </script>
    Where the responce contains somthing like as follows. It is this that is being ignored -

    Code:
    <script>Display this if JS enabled</script>
    Thanks.

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,396
    Thanks
    12
    Thanked 569 Times in 562 Posts
    Doing it like this would require you to eval() the returned code. This will result in executing the code inside the response. But this is not the correct way of doing it (try searching for "eval is evil" in Google)

    Instead you should move the script code to the calling page and execute this code after the response has been received

  • #3
    New Coder
    Join Date
    Jun 2009
    Posts
    62
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I was afraid of that! Unfortunatly thought the <script> chunk of the code is (and needs to be) pretty much slap-bang in the middle, so will have to get my thinking cap on here.

    Thanks for the reply.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Try including the script dynamically when you need it, using this function:
    Code:
    var scriptMount = function(url){
      var js = document.createElement("script");
      js.setAttribute("src",url);
      document.getElementsByTagName("head").item(0).appendChild(js);
    };
    Call it whenever you want in your JavaScript application and it will add a new script to the page asynchronously:
    Code:
    scriptMount('theurltomyscript.js')
    I don't know what you mean with 'slap-bang in the middle' but if you need to build some aspect of the script dynamically on the server based on user interactions then do so by supplying some get parameters in the url.

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Quote Originally Posted by Gardy View Post
    Code:
    <script>Display this if JS enabled</script>
    Thanks.
    Uh . well that is not code
    inside those script tags.
    Why not show the real
    code that is inside the script tags ?
    Executing code inside script tags
    delivered by an xmlhttprequest is
    no prob at all but it must be script
    not "Display this if JS enabled".

  • #6
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    One possibility if you want to shake things up...

    You can create a new script element in the page (using the document.createElement function) and then set the new script element's src attribute to be that AJAX request's requested URL. Then have the URL return just the script without the <script></script> tags.

    That won't really work if you're POSTing with your AJAX though. This would only work for a "GET" request.
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. –G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting

  • #7
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    here is something ...

    index.htm ....

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    <script type="text/javascript">	
    function getIt(){	
    	req=new XMLHttpRequest();
    	req.open("GET","include.htm",false);
    	req.send(null);
    	document.write(req.responseText);}
    </script>
    </HEAD >
    <body>
    <div id="content">
    <script type="text/javascript">	
    getIt();
    </script>
    </div>
    </BODY>
    </HTML>
    include.htm ...

    Code:
    here is some text
    followed by code 
    inside script tags.
    <script>
    alert("JavaScript is enabled")
    </script>
    Of course, this is Sjax.

  • #8
    Regular Coder
    Join Date
    Aug 2010
    Posts
    966
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Here is one that uses
    real ajax ....


    index.htm
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META http-equiv=Content-Type content="text/html; charset=windows-1252">
    <meta name="generator" content="daveyerwin">
    <title>Untitled</title>
    <script type="text/javascript">	
    function init(){	
    	var content = document.getElementById("content");
    	req=new XMLHttpRequest();
    	req.open("GET","mypage.htm",true);
    	req.onreadystatechange = function (){
    				if(req.readyState == 4){
    				populateIframe(content.innerHTML = req.responseText);
    				
    			}		
    	}
    	req.send(null)	
    }
    
    
            function populateIframe(arg) {
                var ifrm = document.getElementById('myIframe');
                ifrm = (ifrm.contentWindow) ? ifrm.contentWindow : (ifrm.contentDocument.document) ? ifrm.contentDocument.document : ifrm.contentDocument;
                ifrm.document.open();
                ifrm.document.write(arg);
                ifrm.document.close();
            }
    
    </script>
    </HEAD >
    <body onload="init()">
    <div id="content">
    
    </div>
    <iframe id="myIframe" style="display:none;"></iframe>
    </BODY>
    </HTML>
    mypage.htm ...
    Code:
    here is some text
    followed by code 
    inside script tags.
    <script>
    alert("JavaScript is enabled")
    </script>


  •  

    Posting Permissions

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