0

I made many pages to be loaded in div area with javascript.

How can I make a direct link from outside of my website to show the last page?

Thank you so much in advance!

The first javascript:

$(document).ready(function() {

    $('#content').load('home.php');


    $('a#nav').click(function() {
        var page = $(this).attr('href');
        $('#content').load('menu/' + page + '.php');
        return false;
    });

});

The second javascript:

$('a#navHan').click(function() {
    var page = $(this).attr('href');
    $('#contentHan').load('menu/Han/' + page + '.php');
    return false;
});
Cilan
  • 13,101
  • 3
  • 34
  • 51

2 Answers2

0

I am not enirely sure what you mean, but I hope this will help.

You can use hashtags. For example: www.example.com/dir#lastpage. Then with javascript, you retrieve the hashtag and load the page with jQuery/Ajax.

var page = location.hash.substr(1);
$('#content').load("menu/" + page + ".php");
Yatoom
  • 307
  • 1
  • 15
  • Thank you so much for your answer! But I'm not sure how to use your code. My website is here: http://korean123.tk/index.php . And I want users to go to a page in the menu, for example, Beginner/Hangeul and Pronunciation/Pronunciation Practice. with a link directly from outside. – Benjamin Kim Jul 13 '14 at 01:37
0

You may try to use hash or query value in the href to your page, and use js to read the value when page loading.


if hash, for example www.example.com#google.com,

var page = window.location.hash.substr(1); // page = google.com

and change your code into:

$(document).ready(function() {
    var fullPage = window.location.hash.substr(1); // page = google.com
    if (fullPage == '')
        $('#content').load('home.php');
    else
        $('#content').load(fullPage);


    $('a#nav').click(function() {
        var page = $(this).attr('href');
        $('#content').load('menu/' + page + '.php');
        return false;
    });

});

the format of your href should be www.example.com#menu/page_name.php or www.example.com#menu/Han/page_name.php


if you use query variables, for example www.example.com?go=google.com

function getParameterByName(name) { // by jolly.exe at http://stackoverflow.com/questions/901115
    name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
    var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
        results = regex.exec(location.search);
    return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
var page = getParameterByName("go"); // page = google.com

and you need to change your code into:

$(document).ready(function() {
    function getParameterByName(name) { // by jolly.exe at http://stackoverflow.com/questions/901115
        name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
        var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
            results = regex.exec(location.search);
        return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
    }
    var page = getParameterByName("page");
    var isHan = parseInt(getParameterByName("han")) == 1;
    if (page == '')
        $('#content').load('home.php');
    else
        $('#content').load('menu/' + ((isHan)? "Han/" : "") + page + '.php');


    $('a#nav').click(function() {
        var page = $(this).attr('href');
        $('#content').load('menu/' + page + '.php');
        return false;
    });

});

the format of your href should be www.example.com?page=page_name or www.example.com?page=page_name&han=1

xhg
  • 1,850
  • 2
  • 21
  • 35
  • Thank you so much for your answer! But I'm not sure how to use your code. My website is here: http://korean123.tk/index.php . And I want users to go to a page in the menu, for example, Beginner/Hangeul and Pronunciation/Pronunciation Practice. with a link directly from outside. – Benjamin Kim Jul 13 '14 at 01:38
  • In your page, include my code as $(function(){...}) – xhg Jul 13 '14 at 05:06