Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9110185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:12:47+00:00 2026-06-17T03:12:47+00:00

I am creating a website where I want to load dynamically the pages through

  • 0

I am creating a website where I want to load dynamically the pages through AJAX. I am using History.js but I think that I am not doing it very right.

Basically I have an index.php page and some other pages: portfolio.php, contact.php and so on. What those additional php files contain is a simple HTML/PHP code that has to dynamically replace the content of a DIV in the index page, called #post-content.

When I click on a menu link in the index page, with a onClick event the JS function showPage(), taking only the page name without “.php” as argument, is called. The function is declared as the following:

function showPage(page) {
    var History = window.History;
    History.pushState(null, page, "index.php?page=" +page);

    $("#post-content").load(page + ".php");
}

In the above function there are also other visual effects implemented, like the fadeIn or fadeOut, but I removed them to make the code more readable here.

The function gives me everything I need, or almost. Testing my website with Google Chrome, if I click a specific link in the menu, for example “portfolio“, I get to ?page=portfolio.php correctly. This does NOT happens if I type manually the URL in the browser (for example, if someone else gives me the direct link to that page). This is because the function showPage() is not called.

So I included an external code above the index.php page:

<?php
if(isset($_GET['page'])) { // Checks if the url contains the "page" parameter.
?>

<script>
$(function() {
    showPage('<? echo htmlentities($_GET['page']); ?>');
});
</script>

<?
}
?>

This gives me the result I want.

At this point I need an expert advice. Am I doing it right? Is this the right way to implement jQuery AJAX inside of a website? I think I’m doing it wrong, very wrong. Coes on the web are very different than mine, however everything looks like working well. What should I do?

Any help in this discussion will be greatly appreciated 🙂 Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T03:12:48+00:00Added an answer on June 17, 2026 at 3:12 am

    First thing i want to say – i said that better load do all this stuff at client side – so you support only one code (on client side).

    But this would lead to multiple ajax calls after page load. You can prevent this by including data into php code/template as in your example, but in this case you need to support also server side code/changes (if you want to generate page from scratch)

    Another way to generate not page but only data that will be fetched via ajax, and include it into page and then use that data with js, instead of doing unnecessary ajax calls.

    So next to code:

    So you can check this fork: https://github.com/llamerr/history.js/commits/master

    As you can see, i simply added frame loading right in init

    //simple function to load iframe (may be done with div and ajax)
    function loadiframe() {
        $('#iframe').load('../index.php');
    }
    

    and

    // Log Initial State
    History.log('initial:', State.data, State.title, State.url);
    //check for state and load what we need
    if (State.data.state == 666) {
        loadiframe();
    }                    
    

    and

    // Bind to State Change
    History.Adapter.bind(window,'statechange',function(){ // Note: We are using statechange instead of popstate
        // Log the State
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        if (State.data.state == 666) {
            loadiframe();
        }                    
        History.log('statechange:', State.data, State.title, State.url);
    });
    

    and

    History.Adapter.onDomLoad(function(){
        console.log('loaded');
        $('#loadiframe').click(function(){
            History.pushState({state:666,rand:Math.random()}, "State 666", "?state=666");
        });
    });
    

    I haven’t looked and Backbone yet, but seems routes(or something like this) is what you need

    http://backbonejs.org/#Router

    var Workspace = Backbone.Router.extend({
    
      routes: {
        "help":                 "help",    // #help
        "search/:query":        "search",  // #search/kiwis
        "search/:query/p:page": "search"   // #search/kiwis/p7
      },
    
      help: function() {
        ...
      },
    
      search: function(query, page) {
        ...
      }
    
    });
    

    I think it’s doing it’s magic, and automatically detects what state must be loaded (upon load or history change etc) and execute needed functions

    Or maybe you can look at this solution – http://haithembelhaj.github.com/RouterJs/

    RouterJs is a simple router for your ajax web apps. It's build upon History.js
    

    So using RouterJs you will have same History.js, but with few more features.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a website where I'm doing ajax requests to load some dynamic content
I'm creating a MetroStyle app and I want to use a website API that
So I am creating a website that I want to have an admin directory
I'm creating a website that people can send sms from my website, I want
I am creating one website where all pages are creating in html but dynamic
I am using Ajax (and jQuery, and also CoffeeScript) to load pages in a
I'm creating a website and I don't want to have user/membership. However, people using
I'm creating a website and I want it also to be prepare for mobile
We want to use Orchard for a website. We are creating a custom module/widget
When creating a website using Asp.Net and using controls such as the ListView is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.