Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get page URL

Status
Not open for further replies.

spyderco

Programmer
Jan 23, 2005
107
US
Sorry if this is a basic question, I don't know JS to any degree.

I have a CGI script located as an SSI include on my web site. Since HTML is stateless, I can't accurately display where current users are visiting (this is a users online script). The best I can do is check the HTTP_REFERER and check the LAST page they were on.

Is there any short and basic JavaScript code that can report back what the current URL is and have it pass that data to a CGI script?
 
one of the ways to access the current url of a page using javascipt is the window.location variable.

Code:
alert( window.location );

*cLFlaVA
----------------------------
[tt]I already made like infinity of those at scout camp...[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
[banghead]
 
Hi.

Thank you for that.

Now I am lost as to how to pass that string over to a server side script. Can you or anyone else give me a little push here?

Thank you!
 
Hi,

Found this in my js library. This should do the trick.

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Tek-Tip Help</title>
<script>
function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  alert('Query Variable ' + variable + ' not found');
}
</script>

</head>

<body> 
<script>
  alert( getQueryVariable("x") );
</script>
</body>
</html>

and you would access the page like this, of course:

Code:
[URL unfurl="true"]http://localhost/test.html?j=dummy&x=75[/URL]

In this case, I am looking to retrieve the x variable. It will display its value in an alert, but you can do whatever you want with it.

Lastly, if you can, I would suggest writing your own "whos online" script in the back end language of your choice. It's an easy code, and great practice.

Regards
 
Without checking the referrer, the best way of passing the current URL to a server-side script would probably be to place the URL in a form field, and submit the form.

You could do it in other ways without a form submission, such as putting it as a parameter after a URL that you retrieve (such as an image or script URL).

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top