how to prevent an element from being reloaded when accessing other pages of the same site?

Asked

Viewed 417 times

2

How to keep an element fixed and without being changed even changing page, on a website.

Like a music player, like websites

http://letras.mus.br/

http://palcomp3.com/

but not with ajax, if at all possible ?

  • Reading the title and the question, I thought the same way as Nickolas Carlos. So much so that the letters leaves the music player fixed when the user uses the scroll on the page. Maybe "how to keep a block always visible/active while browsing a website?" or "how to prevent an element from being reloaded when accessing other pages of the same site?" facilitate the understanding of who will answer. As far as I know: AJAX.

  • Thank you for the remark, correct the question ;)

1 answer

1


If I understand correctly, what you want is to do an AJAX navigation. I know two ways to do this, which is by using AJAX (but you yourself mentioned that you don’t want this) and the other is a sketch using two iframes, I don’t like to use, but it’s a possibility.

Take the example:

iframe {
	display: block;
	width: 100%;
	height: 50%;
	box-sizing: border-box;
	border: none;
}
<iframe src="http://letras.mus.br/charlie-brown-jr/300373/"></iframe>
<iframe src="http://letras.mus.br/mag/1751246/"></iframe>

I’m not a fan of the technique, but it works. It’s the only way I can think of how to do what you want without using AJAX.

@Edit

If you want to do with AJAX, it will depend a lot on the type of page you are working on and everything.

But supposing there’s a menu with multiple links and a div main that will have your content always updated, you could use something like this:

;$(function() {

    var content = $('#conteudo'),
        menuLink = $('#menu a'),
        loading = 'Carregando...';

    // Captura o evento de clique no link do menu;
    menuLink.click(function(event) {

        // Previne que o usuário seja redirecionado para o link;
        event.preventDefault();

        // Troca o HTML da div#conteudo para o texto "Carregando...";
        content.html(loading);

        // Pega o href do link que foi clicado;
        var link = $(this).attr('href');

        // Faz a requisição AJAX;
        $.get(link, function(data) {

            // Pega o conteúdo da div#conteudo na página que será carregada;
            var newContent = $('#conteudo', data ),
                newTitle = $('title', data ).text();

            // Troca o conteúdo atual pelo que foi carregado;
            content.html(newContent);

            // Seta o title da página;
            $('title').text(newTitle);

            // Muda a URL;
            window.history.pushState(null, newTitle, link);

        }).success(function() {

            // Callback para sucesso;

        }).error(function() {

            // Callback para erro;

        });

    });

});

But there goes your need, the code above is very easy to adapt to anything you do with AJAX.

  • I liked the idea of iframe, however, as you said, I’m not a fan either, but you know an ajax technique, that does not leave the URL so polluted ?

  • @Uellingtonpalma, It depends a lot on the context... I’ll leave an example of how I would do something with AJAX for you.

  • Thank you @waghcwb, I will test here, and mark your response.

  • @Uellingtonpalma, Yes, there is. I did an update in the code, now it takes the title of the page and also changes the URL... If you want more 'custom' URL’s, you can use data-attr in your HTML for the URL you want to use. For example: <a href="link" data-url="link-custom">Link</a>. And then use jQuery’s . data() method to read this...

  • @Uellingtonpalma, can you give us the code you are using there? In my tests the code worked well.

  • i had a problem, when I click on a link inside the page loads, which is also with the function of the code above, it reloads the page

  • Is there an error in the console? It should not reload due to Event.preventDefault(); which prevents the link from making its default event (which is reloading the page). You’re doing something wrong...

  • For what I’ve been reading, ajax load, does not load the scripts on the page loaded @waghcwb

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.