0

I have been struggling ajaxing my website, which happens to be a WordPress. What I am trying to do, is to only refresh the content of my blog. What I mean is that my header, footer @ sidebar shouldn't be refreshed when I navigate through my website. It sounded easy to me when I first started, but I was wrong. I've been looking around to find a way to get around problems and found this but it did not help... So, here is my terrible issue :

There are Javascript scripts that are involved in my "refreshed content" and the innerHTML does not keep the JS. Only Html is transposed... As a result, my plugins aren't working anymore.

So, I have been looking for a way to keep the JS content.

I hope I have been clear in desribing my problems and pray for you guys to be able to help me :)

Here is my website : www.construction.urbaineparis.com

If you need more details, I will be very willing to give you the code you need to help. Here is a part of the source that I believe contains the issue.

    //start changing the page content.
        jQuery('#' + AAPL_content).fadeOut("slow", function() {
            //See the below - NEVER TRUST jQuery to sort ALL your problems - this breaks Ie7 + 8 :o
            //jQuery('#' + AAPL_content).html(AAPL_loading_code);

            //Nothing like good old pure JavaScript...
            document.getElementById(AAPL_content).innerHTML = AAPL_loading_code;

            jQuery('#' + AAPL_content).fadeIn("slow", function() {
                jQuery.ajax({
                    type: "GET",
                    url: url,
                    data: getData,
                    cache: false,
                    dataType: "html",
                    success: function(data) {
                        AAPL_isLoad = false;

                        //get title attribute
                        datax = data.split('<title>');
                        titlesx = data.split('</title>');

                        if (datax.length == 2 || titlesx.length == 2) {
                            data = data.split('<title>')[1];
                            titles = data.split('</title>')[0];

                            //set the title?
                            //after several months, I think this is the solution to fix &amp; issues
                            jQuery(document).attr('title', (jQuery("<div/>").html(titles).text()));
                        } else {
                            if (AAPL_warnings == true) {
                                alert("WARNING: \nYou seem to have more than one <title> tag on the page, this is going to cause some major problems so page title changing is disabled.");
                            }
                        }

                        //Google analytics?
                        if (AAPL_track_analytics == true) {
                            if(typeof _gaq != "undefined") {
                                if (typeof getData == "undefined") {
                                    getData = "";
                                } else {
                                    getData = "?" + getData;
                                }
                                _gaq.push(['_trackPageview', path + getData]);
                            } else {
                                if (AAPL_warnings == true) {
                                    alert("WARNING: \nAnalytics does not seem to be initialized! Could not track this page for google.");
                                }
                            }
                        }

                        ///////////////////////////////////////////
                        //  WE HAVE AN ADMIN PAGE NOW - GO THERE //
                        ///////////////////////////////////////////


                        try {
                            AAPL_data_code(data);
                        } catch(err) {
                            if (AAPL_warnings == true) {
                                txt="ERROR: \nThere was an error with data_code.\n";
                                txt+="Error description: " + err.message;
                                alert(txt);
                            }
                        }


                        //get content
                        data = data.split('id="' + AAPL_content + '"')[1];
                        data = data.substring(data.indexOf('>') + 1);
                        var depth = 1;
                        var output = '';

                        while(depth > 0) {
                            temp = data.split('</div>')[0];

                            //count occurrences
                            i = 0;
                            pos = temp.indexOf("<div");
                            while (pos != -1) {
                                i++;
                                pos = temp.indexOf("<div", pos + 1);
                            }
                            //end count
                            depth=depth+i-1;
                            output=output+data.split('</div>')[0] + '</div>';
                            data = data.substring(data.indexOf('</div>') + 6);
                        }

                        //put the resulting html back into the page!

                        //See the below - NEVER TRUST jQuery to sort ALL your problems - this breaks Ie7 + 8 :o
                        //jQuery('#' + AAPL_content).html(output);

                        //Nothing like good old pure JavaScript...
                        document.getElementById(AAPL_content).innerHTML = output;
Community
  • 1
  • 1
Axel R.
  • 1,141
  • 7
  • 22

1 Answers1

0

Change

document.getElementById(AAPL_content).innerHTML = AAPL_loading_code;

to

$("#"+AAPL_content).html(AAPL_loading_code);

jQuery takes care of executing scripts that are in the HTML, which .innerHTML does not do.

I doubt this really breaks in IE 7, as your comment says, unless you're using jQuery 2.x (they've dropped support for old IE versions).

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thank you very much Barmar for your quick answer. I modified the source but unfortunately, it did not fix it. For example, when going from an article back to the homepage, the "MUST SEE" slider disappears. Do you have any idea of what causes this? – Axel R. Dec 25 '13 at 23:45
  • I don't see any mention of a slider in the code you posted, so I have no idea how to answer that. In general, I think you may need to rethink your design -- dynamically loading lots of different Javascript codes is likely to cause conflicts. – Barmar Dec 25 '13 at 23:49
  • Thank you very much for your time Barmar. Do you think the "dataType: "html"" in the code pasted above has to do with the issue? – Axel R. Dec 26 '13 at 00:35
  • I didn't even look at that long AJAX callback, I thought the issue was just the AAPL_loading_code. Looking at the rest of it, the problem might be with all the code that splits the data and constructs `output`. I can't tell what it's doing at a quick glance. – Barmar Dec 26 '13 at 00:42
  • The slider is on this page : [link](http://www.construction.urbaineparis.com) Under the MUST SEE title. I hope you can tell us what goes wrong. Thank you very much for your help Barmar. – Axel R. Dec 26 '13 at 22:12