jQuery replace all HTML

Deadly 提交于 2019-12-23 05:05:29

问题


I'm trying to replace all the HTML (including the HTML tags) with another page. What I'm trying to do is having a website acting as an app even when navigating the another page.

Here is the code :

(function($) {
  Drupal.behaviors.loadingPage = {
    attach: function(context,settings) {
      $('a').click(function(event) {
        event.preventDefault();
        // Create the loading icon
        // ...
        $.ajax({
          url: $(this).attr('href'),
          success: function(data) {
            $('html').replaceWith(data);
          }
        });
      });
    }
  };
})(jQuery);

I've tried several things. replaceWith() causes a jQuery error in jquery.js after deleting the HTML tag, I guess it is because it can't find the parent anymore to add the replacement.

The best result I got was with document.write(data). The problem is, the javascript code on the loaded page is not executed.

Anyone got a better idea ?


回答1:


A better idea? Yeah, just load the new page normally by setting window.location instead of using AJAX. Or submit a form.

Otherwise, if you want to load something to replace all of the visible content of the current page but keep the current page's script going, put all the visible content in a frame sized to fill the browser window - which, again, you wouldn't populate using AJAX.

(I like AJAX, but I don't see why you'd use it to replace a whole page.)



来源:https://stackoverflow.com/questions/7620669/jquery-replace-all-html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!