0

I'd like to reload a js in a certain div on click of a button without reloading the complete page. It is also possible to just reload the div in which the js is loaded.

I've alread tried several solutions like: http://www.philnicholas.com/2009/05/11/reloading-your-javascript-without-reloading-your-page/

or some ideas of other posts like the following from here Refresh/reload the content in Div using jquery/ajax

Using this code for example:

$("#content").load(location.href + " #content");

only the div but not the script is reloaded and nothing is shown.

Using an ajax request, it puts me all the js-code into the div as text.

Do you have any further idea?

EDIT: The script that is loaded shows content, depending on some value of a session. Clicking the button changes the value which should result in another content in the div running the script.

EDIT 2: The current state is reloading the entire page: This is the div where additional content (other divs and stuff) is added by the script:

            <div id="dynamic_content">
                 <script src="myjs.js"></script>
            </div>

And here is the button for changing the view of the content:

            <div id="change_view_mode" onclick="change_view()">                 
                <script> 

                    if(window.SESSION.view_mode == "simple"){
                        document.getElementById("change_view_mode").innerHTML = "complex tree";

                    }else{
                        document.getElementById("change_view_mode").innerHTML = "simple tree";
                    }

                </script>
            </div>

And here the function for changing the view where I tried to implement the reload of the content:

                //toggle view and reload the page
                function change_view(){
                        if(window.SESSION.view_mode == "simple"){

                            $.ajax({
                                    url:'set_view_mode.php',
                                    async: false,
                                });
                            //$("#dynamic_content").load(location.href + " #dynamic_content");
                            location.reload(true);
                        }else{

                            $.ajax({
                                    url:'set_view_mode.php',
                                    async: false,
                                });
                            //$("#dynamic_content").load(location.href + " #dynamic_content");
                            location.reload(true);
                        }
                    }

I change the view_mode in the session by calling a small php-file. Then I tried several things as mentioned (one is left in the code as comment). This is done depending on the current view. So its just toggle the view from one to the other.

Community
  • 1
  • 1
Maki
  • 109
  • 13

1 Answers1

0

I have create a git repository for your answer, can you please try this way: https://github.com/RobinHossain/ajax-page-load

Live Demo Link: http://ajax-page-load.w3bd.com/

Robin Hossain
  • 711
  • 9
  • 12