tags:

views:

25

answers:

3

Hi,

i have two files. In first (parent, address: www.myaddress.com/fun/test.php) is jQuery function:

<script type="text/javascript">         
$(function(){
    $(document).ready(function()
    {
        import_f = setInterval(function()
        {
            $('#file').load('fun2.php?id='+ Math.random());

        }, 5000);       
    });
});         
</script>

When i print in fun2.php file full page address, i get: /fun2.php?id=0.31089064538474454.

How get paren page address. (www.myaddress.com/fun/test.php).

Parent's address may always be another, so I need to find a function that.

Thanks

+1  A: 

is this what your looking for?

function getParentAddress(){
  return window.location;
}
Andy Groff
A: 

The current address of the page is stored in the window.location property.

console.log(window.location)

run on this page yields

hash: ""
host: "stackoverflow.com"
hostname: "stackoverflow.com"
href: "http://stackoverflow.com/questions/4031148/php-jquery-load-function-and-page-address"
origin: "http://stackoverflow.com"
pathname: "/questions/4031148/php-jquery-load-function-and-page-address"
port: ""
protocol: "http:"
search: ""
Alex JL
+1  A: 

Sounds like you are looking for the HTTP Referrer. You can get it via $_SERVER['HTTP_REFERER'].

But note that this can be changed by the user to something else, so you actually cannot rely on that.


Maybe it is better to send the current URL via the Ajax call, along with the ID:

$('#file').load('fun2.php?id='+ Math.random() +'&parent=' + encodeURIComponent(window.location));

But again, if one really wants to, he can change this too. So you can never 100% trust that one request is really coming from a certain page.

Reference: window.location, encodeURIComponent

Felix Kling