views:

84

answers:

6

I have this code in my HTML:

<h3 id="left">Lorem Ipsum </h3>

                <h3 id="right">[Current URL Here]</h3>

I want to display (dynamicly) the current URL inside the <h3> tags. I've been trying to figure it out for a few days, but I'm really a mobile developer, not an HTML developer, so it's proven difficult. I need this for an app I'm working on, so Please go easy on me :)

Thanks in advance.

+5  A: 
document.getElementById('right').innerHTML = window.location.href;
MooGoo
Beat me to it :)
Thqr
....... same :)
vol7ron
+1  A: 

Well, you simply cannot do it in pure HTML.

With javascript, you can go with

<h3 id="right">
<script type="text/javascript">
document.write(location.href);
</script>
</h3>

Otherwise, if you are requesting a page on the server, you should rather have it done in there.

Guillaume Bodi
MooGoo's javascript is a cleaner way to handle this.
Guillaume Bodi
A: 

Hi,

the php code for getting complete url of current page is as follows

<?php 

    $protocol = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';

    echo $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
?>

Reference

if you want to use javascript use the method suggested by @MooGoo

full usage of that script is as follows

<SCRIPT LANGUAGE="JavaScript">        
   document.getElementById('right').innerHTML = window.location.href;       
</SCRIPT>

use this after you declared/defined <h3 id="right">[Current URL Here]</h3>

Hope helpful

srinivas
-1 for not mentioning to a newb that they should output `$_SERVER['REQUEST_URI']` through `htmlspecialchars`. See here:http://stackoverflow.com/questions/3446459/what-is-the-benefit-of-using-the-super-global-serverphp-self-in-php/3446566#3446566 The same principles apply.
aaronasterling
+1  A: 

If you wanted to do it in PHP, it's a little more involved:

$url = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url .= $_SERVER['HTTP_HOST'] . htmlspecialchars($_SERVER['REQUEST_URI']);

As aronasterling points out, you need to sanitize $_SERVER['REQUEST_URI'] to prevent XSS.

Mark Trapp
A: 

While the JavaScript are what is more common, you could also use Server-Side Includes:

<h3 id="right">
    <!--#echo var="SERVER_NAME" -->/<!--#echo var="DOCUMENT_URI" -->
</h3>
  • instead of SERVER_NAME you can try HTTP_HOST
  • instead of DOCUMENT_URI you can try REQUEST_URI; one includes the query string, the other doesn't
vol7ron
-1 see my comment on every other answer in this thread that involves php.
aaronasterling
@aaronasterling: Read the difference between `DOCUMENT_URI` and `REQUEST_URI`. If asker wanted to avoid XSS, he should use the appropriate one. If asker is on an intranet or using the page for personal use, then XSS is not a threat.
vol7ron
This is why people should have to submit a resume before given vote capability
vol7ron
A: 

Hi,

Php Code:

function curPageURL() {
 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
}

<h3 id="right">echo curPageURL();</h3>
boss
-1 see my comment srinivas answer.
aaronasterling