Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 19, 2011 @ 15:07
    Jeroen Breuer
    0

    Use jQuery to load a page with another template

    Hello,

    I've got a page and I need to rewrite the page with another template using jQuery. I must stay on the same page so loading with another template using "/TemplateName" or "?alttemplate=TemplateName" in the url is not an option. I can't go to a different url because I use jQuery Address. I load different pages dynamically with url's that are after the #. So in fact I'm always on the same page, but the content is changed by loading a different template. So my question is how can I rewrite a complete page using jQuery? After the page is rewritten the rewrite code still needs to be availble so it can rewrite to the next template again. Any suggestions?

    For the people who think this is not SEO frienly I use the AJAX crawlable technique from Google: http://code.google.com/web/ajaxcrawling/docs/getting-started.html

    Jeroen

  • Pickels 75 posts 108 karma points
    Jan 19, 2011 @ 15:23
    Pickels
    0

    You can load content from an other page by using jQuery load. Using load is probably not a very efficient way of doing this but it all depends a little on what exactly you are trying to do.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 19, 2011 @ 15:29
    Jeroen Breuer
    0

    I've looked at jQuery load, but you can only load you html into another div. I somehow need to replace all the html of the page with the new html instead of inserting it in a div. The html from the other template already has the head and body tag.

    Jeroen

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 19, 2011 @ 15:40
  • Pickels 75 posts 108 karma points
    Jan 19, 2011 @ 15:41
    Pickels
    0

    I guess just changing the body of the html page is enough? If it is you can just do $('body').load('foo/bar.aspx'). If you need to adjust the new template before loading it you should be able to make an ajax call to the page. This should return the markup if I am correct.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 20, 2011 @ 15:48
    Jeroen Breuer
    0

    Thanks for the info. I decided not to rewrite the complete html, but load the <head> and <body> tag with the html from the other page. Here is my code:

    <script type="text/javascript">
        SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);
    
        function handleChange(event) {
    
            //Get the url and remove the ! at the beginning.
            var url = SWFAddress.getPath().substr(1);
    
            if (url != '' && url != '/') {
                loadPage(url);
            }
        }
    
        function loadPage(url) {
    
            //This post get's the url of the page we need to download.
            $.post(SWFAddress.getBaseURL() + '/base/Digibiz/GetHtmlUrl/', { url: encodeURIComponent(url) }, function (htmlUrl) {
    
                //Download the page to get the html.
                $.get(htmlUrl, function (html) {
    
                    //TODO remove previous added head content.
    
                    //Get the head element from the downloaded html and add it to this head.
                    var headHtml = /<head.*?>([\s\S]*)<\/head>/.exec(html)[1];
                    $("head").append(headHtml);
    
                    //Get all the content of the load div from the downloaded html (= all content) and place it in our load div.
                    //I first tried to place the body from the downloaded html in the body of this page, but it gave some error with SWFAddress.
                    var load = /<div id="load".*?>([\s\S]*)<\/div>/.exec(html)[1];
                    $("#load").html(load);
    
                    //Get the title from the downloaded html and set it.
                    var title = /<title.*?>([\s\S]*)<\/title>/.exec(headHtml)[1];
                    SWFAddress.setTitle(title);
                });
            });
        }
    </script>

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 20, 2011 @ 18:24
    Jeroen Breuer
    0

    As you can see in the code there's a TODO which says I need to remove the elements I previously added to the head. Does anybody have an idea how I can do that? I download the head of a page which has a title and meta data. If I download another page I need to remove the first title and meta data before appending the new title and meta data. Any idea how I can do that? Sometimes it's not just the title and meta data, but also a stylesheet so it's flexible.

    Jeroen

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 20, 2011 @ 20:00
    Jonas Eriksson
    1

    I might not understand your task, but a simple $('meta').remove() will remove all <meta> from the doc.

    Here's a nice tool to experiment with: js + jQuery http://jsbin.com (besides Firebug)

    Cheers

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 20, 2011 @ 23:39
    Jeroen Breuer
    0

    Hi Jonas,

    Thanks for the tool seems very useful :).

    $('meta').remove() won't work because it will only delete all the meta tags, but the downloaded head can also contain style and script tags. It's variable per template/page I try to download. So somehow I need to remember all the tags I added from the previous downloaded template/page and remove those before I insert the head tag of another downloaded template/page. I can't just clear the complete head like I do with the body, because it would also delete the javascript used to download another head and body tag. You understand :).

    Jeroen

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 21, 2011 @ 08:25
    Jonas Eriksson
    1

    Hi Jeroen,

    ah, yes, I kinda figured it was something I missed :) .

    Can you add a class on your existing tags? In that case you could do a filtered remove with the :not selector:

    <script class="keep" src="..."></script>
    $('head :not(.keep)').remove();

    http://jsbin.com/ocudi3/11/edit

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 21, 2011 @ 08:53
    Jeroen Breuer
    0

    Hi Jonas,

    That sounds like a great idea! I'll give it a try and let you know if it works :). Thanks.

    Jeroen

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jan 24, 2011 @ 09:49
    Jeroen Breuer
    0

    Hi Jonas,

    I've just tested your sample at it works perfect :). Thanks!

    Jeroen

  • Jonas Eriksson 930 posts 1825 karma points
    Jan 24, 2011 @ 09:50
    Jonas Eriksson
    0

    Cool, jQuery rocks,

    Cheers

    Jonas

  • Tom 713 posts 954 karma points
    Jul 16, 2012 @ 14:43
    Tom
    1

    try http://hashbang.envoyat.com/ it uses umbraco and documents how to do exactly this.

Please Sign in or register to post replies

Write your reply to:

Draft