Single Page Application with jQuery

Asked

Viewed 3,120 times

2

How can I develop a SPA with jQuery? I did with Angularjs with the following code, but I’m having difficulties to migrate to it definitively and decided to stick with jQuery not to be late.

var myapp = angular.module('myapp', []);
myapp.config(function($routeProvider) {
$routeProvider
.when('/:acess', {
templateUrl : 'sources/default.html',
controller  : 'myapp-controll'
})
.otherwise({ redirectTo: '/welcome' });
});

myapp.controller('myapp-controll', function($scope, $routeParams) {
$scope.templateUrl = './g_c.php?a='+$routeParams.acess; 
});

This code carried my pages without leaving the original page, using #

Explaining better:

Let’s pretend there’s a file that way:

./pages/ (pasta) ----
index.html (este)   |
                     --/> home.html
                    |
                     --/> produtos.html

By clicking on Pagina inicial he has to click on the div "content" all the contents of the requested file, ie he would have to take only the home and pull him out of the briefcase pages playing all the content in the div and loading it completely.

But what? If I want to share the link, I will have to teach the user how to check up to it? Which buttons to click? This would cause the site to lose many users and access, so the answer is no!

Or Whether I provided the link http://meusite/#/produtos/ for someone, when accessing it has to click on the div "content" the content of the page produtos automatically, without having to click anything.

<div id="inicio"><a href="#/home/">Pagina inicial</a> | <a href="#/produtos/">Produtos</a> | </div>

<div id="conteudo">Oi, eu sou a pagina inicial</div>
  • What language is on the server side?

  • Use PHP to handle my data on the server.

  • It’s late for me (European zone). I deleted my answer because it was clear after your last explanation that you need to use the .load(). I will add your info to the question. I begin to wonder if it would not be better to use Angular itself and learn better how it works?

  • Angular is complicated for me, I would even learn but the time is short. Wouldn’t it be possible what I explained in jQuery? @Sergio

2 answers

6


Okay, approach with .load():

$(function () {
    $(window).on('hashchange', hashchanged);
    hashchanged();
});

function hashchanged() {
    var hash = location.hash.replace(/[#\/]/g, '') || 'home';
    $("#conteudo").load(hash + '.html');
}

The first part of the code will look for changes to the URL. When the URL changes ( and when you first load the page) the function hashchanged will be racing.

This function will filter the symbols # and / and use this information to load another page using .load() jQuery.

  • Perfect! I thought it was somewhat more complicated, so this function would be fulfilling the concept of SPA? @Sergio

  • @user3163662 SPA means single-page application, this code does it. However Angular would be more complete (and perhaps complex).

  • @user3163662 tried my answer? If you have probables with this plugin ask a question about it stating which part you can’t get to work. If you want a simple solution, I think my answer will do. I think by now I’ve learned to use Angular and do what Angular is good at.

  • 1

    your code worked perfectly, it would not be worth changing all my functions pro angulaejs just to have the SPA and with jQuery we managed perfectly. Thank you!

0

In pure jQuery is complicated to be done, but as you are in a hurry to accomplish, you may need not to use Angularjs itself, but to look for some simpler and faster alternative: https://github.com/c-smile/spapp

By describing the project within Github, you learn the few lines that are needed to develop the application you want (routing is done automatically, you just need to set the page name and the file url to be rendered in the menu item).

  • the question is specific to jQuery, unfortunately this answer does not answer the question :/

Browser other questions tagged

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