Echo javascript variable?!?!

Not sure if this should be in the PHP forum or here but here goes…

I’m trying to get this to work but with no luck…

<script type="text/javascript">
var lala = top.location.href; 
</script>

<?
$topurl = $_GET['lala'];
echo 'Whats the url:'.$topurl;
?>

What I’m trying to accomplish is to get the parent url from within an iFrame using PHP… I have surfed around the net and found that using javascript would work, but I’m not getting the javascript variable into the PHP…? Why? and are this the right way to go???

Thanks in advance :slight_smile:

Your code will not work becouse PHp is prossed before jour jaascript starts.
So your PHp part will loas first.

what you can do is sen the data via a redirect.

howevver what is the aim of knowing the top location?

Is’nt the javascript running before the php?

Not sure what you mean?!?!

First you need to know that PHP code is ran at the server side;
(so everything between <? ?> is executed before your browser starts reading the page.

When the page is read, your browser runs all scripts between <script> tags. (ans parses HTML)

How can I change this?

The reason for all this is that I’m trying to make my page available on facebook to. And as I’m making my page a FB app using iFrame I would like to know if the user is getting to my page through facebook og just as my plain http://www.mypage.com

So I need to know if the page is as a child or as a parent… Does this make sense!!!

JavaScript has no ability to see anything regarding separate frames if the domain is different.

You might be able to tell where your visitor is from by checking the referer header in PHP (assuming your visitor hasn’t turned it off)

Well, if I use this:

alert(top.location.href);

I get an “undifined” if it is in facebook (child) and the page url (www.mypage.com) if its parent… That is allright for me. Then I can set some rules if the page is undifined… But I still dont’ know how to get the javascript variable down as a php variable…

Any ideas?