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 9053495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:26:08+00:00 2026-06-16T13:26:08+00:00

I’m working with AJAX on a website and I’m currently making some pages to

  • 0

I’m working with AJAX on a website and I’m currently making some pages to load on a certain div: “pageContent”. Now I have another content I want to be opened on another div: “reproductor”. I want to open ‘page’ in ‘pageContent’ div and ‘play’ in ‘reproductor’ div. I don’t know how to modify my script.js and load_page.php files in order to make it work. Here’s what I got:

HTML:

<script type="text/javascript" src="js/script.js"></script>
<a href="#page1">PAGE</a>
<a href="#play1">PLAY</a>
<div id ="pageContent"></div>
<div id="reproductor"></div>

script.js:

var default_content="";
$(document).ready(function(){

    checkURL();
    $('ul li a').click(function (e){
        checkURL(this.hash);
    });

    default_content = $('#pageContent').html();
    setInterval("checkURL()",250);

});

var lasturl="";
function checkURL(hash)
{
    if(!hash) hash=window.location.hash;

    if(hash != lasturl)
    {
        lasturl=hash;
        if(hash=="")
            $('#pageContent').html(default_content);
        else
        loadPage(hash);
    }
}

function loadPage(url)
{
    url=url.replace('#page','');
    $('#loading').css('visibility','visible');
    $.ajax({
        type: "POST",
        url: "load_page.php",
        data: 'page='+url,
        dataType: "html",
        success: function(msg){
            if(parseInt(msg)!=0)
            {
                $('#pageContent').html(msg);
                $('#loading').css('visibility','hidden');
            }
        }   
    });
}

load_page.php:

<?php

if(!$_POST['page']) die("0");

$page = (int)$_POST['page'];

if(file_exists('pages/page_'.$page.'.html'))
    echo file_get_contents('pages/page_'.$page.'.html');
else 
    echo 'There is no such page!';
?>

I forgot to mention: I have my ‘pages’ content in a folder named ‘pages’ and my ‘play’ content in another named ‘plays’.
Thanks for your help!

  • 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-16T13:26:10+00:00Added an answer on June 16, 2026 at 1:26 pm

    If I understand, your have two groups of links (for pages and a play list) each one to be loaded in a different container. Here is something you can try: mainly I eliminated the global variables and put the current hash inside each containter’s data, and separated the management of the two groups of links.

    In this code I supposed you have a separate load_play.php file. If not, then you can use the same page for both kind of links, but you’ll have to merge loadPlay with loadPage, change loadPage(newHash) to loadPage(newHash, linkType) and change the ajax parameter from 'page='+newHash to 'number='+newHash+'&type='+linkType, and do the corresponding changes server side in your PHP page. I would recommend you to create two separate PHP files in order to manage the two types of content.

    I remember you where doing something with the hash of the current page’s url, you can still set it in the ajax’s success, inside the loadPage function.

    Here is a working sfiddle example with some console calls (open browser’s console) but without the ajax call.

    UPDATE:

    I updated the code, so your can manage the dynamically added links (new content loaded via AJAX) and fixed the management of urls with hashes, which was broken because of the new code.

    <div id="#page">
        <a href="#page1" class="pageLink">PAGE 1</a>
        <a href="#page2" class="pageLink">PAGE 2</a>
        <a href="#play1" class="playLink">PLAY 1</a>
        <a href="#play2" class="playLink">PLAY 2</a>
        <a href="#play3" class="playLink">PLAY 3</a>
        <div id ="pageContent"></div>
        <div id="reproductor"></div>
    </div>
    

    And this is the javascript:

    $(document).ready(function(){
        $('#pageContent').data('currentPage', '');
        $('#reproductor').data('currentPlay', '');
    
        //This will allow it to work even on dynamically created links
        $('#page').on('click', '.pageLink', function (e){
            loadPage(this.hash);
        });
    
        $('#page').on('click', '.playLink', function (e){
            loadPlay(this.hash);
        });
    
        //And this is for managing the urls with hashes (for markers)
        var urlLocation = location.hash;
        if(urlLocation.indexOf("#page") > -1){
            $('.pageLink[href='+ urlLocation +']').trigger('click')
        }
    
    });
    
    function loadPage(newHash)
    {
        //This is the current Page
        var curHash = $('#pageContent').data('currentPage');
        //and this is the new one
        newHash = newHash.replace('#page', '');
    
        if(curHash===newHash){
            //If already loaded: do nothing 
            return
        }
    
        $('#loading').css('visibility','visible');
        $.ajax({
            type: "POST",
            url: "load_page.php",
            data: 'page='+newHash,
            dataType: "html",
            success: function(msg){
                if(parseInt(msg)!=0)
                {
                    $('#pageContent').html(msg).data('currentPage',newHash);
                    $('#loading').css('visibility','hidden');
                }
            }   
        });
    }
    
    function loadPlay(newHash)
    {//Similar to loadPage...
        var curHash = $('#reproductor').data('currentPlay');
        newHash = newHash.replace('#play', '');
    
        if(curHash===newHash){return}
    
        $('#loading').css('visibility','visible');
        $.ajax({
            type: "POST",
            url: "load_play.php",
            data: 'play='+newHash,
            dataType: "html",
            success: function(msg){
                if(parseInt(msg)!=0)
                {
                    $('#reproductor').html(msg).data('currentPlay',newHash);
                    $('#loading').css('visibility','hidden');
                }
            }   
        });
    }
    

    Check this and comment if this is what you need, or I got something wrong 🙂

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

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.