Forum Moderators: open

Message Too Old, No Replies

use js to echo page url in code?

         

MarianneC

3:15 pm on Jun 24, 2005 (gmt 0)

10+ Year Member



I suspect that there's a way to do this. I have some code on the page that is currently hard coded to reference the particular page for a print and email page function.

I would love to change the code to be generic so that the base page name (i.e. "0104shesaid" for 0104shesaid.html) gets echoed onto the page as it's created.

Does any one have any code snippets or sample pointers on how to do this?

Many thanks!
Marianne

Rambo Tribble

1:10 pm on Jun 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can derive the URL of the current page by querying location.href. You can use string methods to parse out the address fragment you want. Here's a q'n'd example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
</head>
<body>
<script type="text/javascript">
var loc_var=window.location.href;
alert(loc_var);
var loc_var2=loc_var.split('.');
var lv_ln=loc_var2.length;
var loc_var3=loc_var2[lv_ln-2];
var loc_var4=loc_var3.split('/');
lv_ln=loc_var4.length;
alert(loc_var4[lv_ln-1]);
</script>
</body>
</html>