1

I am using an AJAX call to a PHP file from another PHP file. The called PHP creates an html content alongwith a JAvascript code :

<div id = "chart" style="height: 400px "></div>
<script src="http: //ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src = "http://code.highcharts.com/highcharts.js" > < /script>
<script>
    $(function (){ 
        var xcat = ["Build 1","Build 2"];
        var datas = [100,60]; 
        $("#chart").highcharts({
            chart: {
                type: "column"
            },
            title: {
                text: "Build Progress"
            },
            xAxis: {
                categories: xcat
            },
            yAxis: {
                title: {
                    text: "seconds"
                }
            },
            series: [{
                name: "Boot Time",
                data: datas
            }]
      });    
});
</script >

The script does not get executed after inserted using innerhtml. I read some solutions: And one of them is present but for an external js file.

http://www.hunlock.com/blogs/Howto_Dynamically_Insert_Javascript_And_CSS#quickIDX1

I need to know how to make the script run without an external js file

Help me with the same

Thanks. Patrick

1
  • may be its only because of this typo: < /script> , must be: </script> Commented Jun 20, 2013 at 6:04

2 Answers 2

1

When you use innerHTML, the script tags are not executed. If you use jquery, you can try $.html() instead of innerHTML. $.html() will automatically execute script tags.

Sign up to request clarification or add additional context in comments.

Comments

1

The code you have linked will only fire when the DOM ready event fires. If you are loading this via ajax, you're right. It'll never fire because the page is already loaded and you aren't refreshing.

change

    $(function (){ //shorthand for jQuery(document).ready(function(){
        var xcat = ["Build 1","Build 2"];
        var datas = [100,60]; 
        $("#chart").highcharts({
            chart: {
                type: "column"
            },
            title: {
                text: "Build Progress"
            },
            xAxis: {
                categories: xcat
            },
            yAxis: {
                title: {
                    text: "seconds"
                }
            },
            series: [{
                name: "Boot Time",
                data: datas
            }]
        });    
    });

to

    function (){ 
        var xcat = ["Build 1","Build 2"];
        var datas = [100,60]; 
        $("#chart").highcharts({
            chart: {
                type: "column"
            },
            title: {
                text: "Build Progress"
            },
            xAxis: {
                categories: xcat
            },
            yAxis: {
                title: {
                    text: "seconds"
                }
            },
            series: [{
                name: "Boot Time",
                data: datas
            }]
        });    
    }();// the () self executes the function so you don't have to call it

6 Comments

I tried to use the self executing function but that doesnt seem to still do the trick.. When i run the called PHP all by itself it loads perfectly using my approach, but after using the self executing one even that seems to fail
did you fix the typo @cherniv pointed out also? < /script> should be </script>
@Patrick can I assume you are using $.ajax? if so, are you passing a dataType: 'html' parameter? According to jquery documentation " "html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM. " more here
I am using the XmlhttpRequest method as explained below: w3schools.com/php/php_ajax_database.asp not proper ajax. I am not sure how to do it using AJAX and JQUERY. :(
@Patrick I have a feeling its the content header thats not telling the browser to execute the script (although i still stand by the self executing function in my original answer as part of the fix to the overall problem) give jquery ajax a try when you get a chance. It'll make your life easier if you are already using the library for other things. http://api.jquery.com/jQuery.ajax/
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.