By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,712 Members | 2,111 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,712 IT Pros & Developers. It's quick & easy.

Capturing current page's URI?

P: n/a
I am wondering if the following can be accomplished with javascript
(and if so, if you think javascript is an appropriate solution). I'm
afraid my javascript skills lack. I need to build a hyperlink that
includes the current web page URI. When clicked, the current page is
then processed by a program. For example, it would look something
like this:

<a href="http://some.program.com/index.php?current-web-page-URI">text
etc.</a>

where "current-web-page" would be the full URI,
http://whatever.com/stuff/index.html

Resulting in:

<a
href="http://some.program.com/index.php?http://whatever.com/stuff/index.html">text
etc.</a>

Thus I need a way to grab the current web page's URI for the
hyperlink. This hyperlink would be on numerous different pages and is
why the page's URI will differ.

They're not PHP generated pages, though that thought is a possibility,
else I would use something like PHP's $_SERVER['REQUEST_URI']

While a server side solution might be preferable to a client side
solution, people using the site don't have javascript turned off
because of other javascripts at the site.

Therein is my situation. If anyone has comment or suggestions on how
to script this, I would be much obliged.

TIA
Oct 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Sandy Bremmer said the following on 10/12/2005 11:07 PM:
I am wondering if the following can be accomplished with javascript
(and if so, if you think javascript is an appropriate solution). I'm
afraid my javascript skills lack. I need to build a hyperlink that
includes the current web page URI. When clicked, the current page is
then processed by a program. For example, it would look something
like this:

<a href="http://some.program.com/index.php?current-web-page-URI">text
etc.</a>

where "current-web-page" would be the full URI,
http://whatever.com/stuff/index.html

Resulting in:

<a
href="http://some.program.com/index.php?http://whatever.com/stuff/index.html">text
etc.</a>


Have PHP generate the links and include the current URL.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Oct 13 '05 #2

P: n/a
"Sandy Bremmer" <no_email_please@prefer_the_newsgroups.com> wrote in message
news:kk********************************@4ax.com...
I am wondering if the following can be accomplished with javascript
(and if so, if you think javascript is an appropriate solution). I'm
afraid my javascript skills lack. I need to build a hyperlink that
includes the current web page URI. When clicked, the current page is
then processed by a program. For example, it would look something
like this:

<a href="http://some.program.com/index.php?current-web-page-URI">text
etc.</a>

where "current-web-page" would be the full URI,
http://whatever.com/stuff/index.html

Resulting in:

<a
href="http://some.program.com/index.php?http://whatever.com/stuff/index.html
">text etc.</a>

Thus I need a way to grab the current web page's URI for the
hyperlink. This hyperlink would be on numerous different pages and is
why the page's URI will differ.

They're not PHP generated pages, though that thought is a possibility,
else I would use something like PHP's $_SERVER['REQUEST_URI']

While a server side solution might be preferable to a client side
solution, people using the site don't have javascript turned off
because of other javascripts at the site.

Therein is my situation. If anyone has comment or suggestions on how
to script this, I would be much obliged.

TIA


Will this help? Watch for word-wrap.

<script type="text/javascript">
document.write("<a href='http://some.program.com/index.php?" + location.href
+ "'>link</a>");
</script>
Oct 13 '05 #3

P: n/a
On Wed, 12 Oct 2005 23:20:46 -0500, "McKirahan" <Ne**@McKirahan.com>
wrote:
"Sandy Bremmer" <no_email_please@prefer_the_newsgroups.com> wrote in message
news:kk********************************@4ax.com.. .
I am wondering if the following can be accomplished with javascript
(and if so, if you think javascript is an appropriate solution). I'm
afraid my javascript skills lack. I need to build a hyperlink that
includes the current web page URI. When clicked, the current page is
then processed by a program. For example, it would look something
like this:

<a href="http://some.program.com/index.php?current-web-page-URI">text
etc.</a>

where "current-web-page" would be the full URI,
http://whatever.com/stuff/index.html

Resulting in:

<a

href="http://some.program.com/index.php?http://whatever.com/stuff/index.html
">text
etc.</a>

Thus I need a way to grab the current web page's URI for the
hyperlink. This hyperlink would be on numerous different pages and is
why the page's URI will differ.

They're not PHP generated pages, though that thought is a possibility,
else I would use something like PHP's $_SERVER['REQUEST_URI']

While a server side solution might be preferable to a client side
solution, people using the site don't have javascript turned off
because of other javascripts at the site.

Therein is my situation. If anyone has comment or suggestions on how
to script this, I would be much obliged.

TIA


Will this help? Watch for word-wrap.

<script type="text/javascript">
document.write("<a href='http://some.program.com/index.php?" + location.href
+ "'>link</a>");
</script>


Yes, that works. I will also explore a PHP solution but the
javascript is working. Thanks!

Oct 13 '05 #4

P: n/a
Sandy Bremmer wrote:
On Wed, 12 Oct 2005 23:20:46 -0500, "McKirahan" <Ne**@McKirahan.com>
wrote:

[...]
<script type="text/javascript">
document.write("<a href='http://some.program.com/index.php?" + location.href
+ "'>link</a>");
</script>


Yes, that works. I will also explore a PHP solution but the
javascript is working. Thanks!


You could use an onload function that adds the current URI to every A
element's href attribute - a lot cleaner than using a script element
for every A element (and the links will still work without scripting,
although without the URI extension).
function addURI()
{
var allLinks = document.links;
var thisURI = location.href;
var x;
for (var i=0, j=allLinks.length; i<j; ++i){
x = allLinks[i];
if (x.href && '' != x.href){
x.href += '?'+ thisURI;
}
}
}

window.onload = addURI;


--
Rob
Oct 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.