Hi there all,

I want to grab information from an url with anchors and I suppose this can't be done with php, as ive been told and that it would have to be done with javascript.

I am wondering if anyone knows the code to grab the url and anchor inside the url and paste it to a textbox?

Ie. www.mysite.com/#a_id=joe&b_id=11111

Then to paste this url when someone goes to it into a textbox along with the anchor parameters.

anchor tag looks like this:

<a id="anchor_1" href="www.mysite.com/#a_id=joe&amp;b_id=11111" >click to visit (something like this)</a>

now to get the href property (url) you need a reference to anchor tag. That can be done in javascript like this:-

var anchor1 = document.getElementById('anchor_1');
var url = anchor1.href;

here url is the href property of anchor tag with id = 'anchor_1'

If you want to get url and paste it in textbox when someone goes over any anchor here is simple code:

		function pastUrl(aElement) {
			var url = aElement.href;
			var textBox = document.getElementById('urlText');
			textBox.value = url;
		<a href='http://www.google.com' onmouseover='pastUrl(this)'>Google</a> <br>
		<a href='http://www.google.com/search?hl=en&q=anchor&btnG=Search&meta=&aq=f&oq=' onmouseover='pastUrl(this)'>Google with params</a><br>
		<a href='http://www.yahoo.com' onmouseover='pastUrl(this)'>Yahoo</a><br>
		<input type='text' id='urlText' style='width:400px;'/>

Well you could do it either in PHP and Javascript or just Javascript. In PHP you would use $_SERVER['REQUEST_URI'] to get the request URI (who'd a thunk it?) and then give that to javascript to "paste" into the textbox. In purely Javascript you could use window.location.href as the value to put into the textbox.


Thanks for the reply, I will try the code from luckychap see if I can get it working.

Here's what I tried first with php.

<input type="text" name="t3" id="t3" value="<?php $url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; echo $url; ?>" />

It only grabs upto the # and then doesnt get anything after that. I will try with javascript and see how it goes


Hi again,

I decided to use windows.location as I have no control on what information I can put in the links themselves.

<script type="text/javascript">
	  var currentURL = window.location;
( currentURL.href );
( currentURL.hash );


This is the code im messing with, I know nothing about javascript be the way, but how would I get the 2 elements "href" and "hash" to pass into a textbox?
How and where would I put an id to match with a textbox, so it is autofilled when the page loads?


How and where would I put an id to match with a textbox, so it is autofilled when the page loads?

You can put this code on page load:

<body onload="pasteUrl();">

As mentioned in my previous post:

function pasteUrl() {
    url = window.location;
    var textBox = document.getElementById('urlText');
    textBox.value = "href: " + ( url .href ) + "  hash: " + ( url .hash );


Thanks luckychap, works perfectly. Sorry i didnt get it the first time round', but i need "javascript for dummies" - thanks a lot for the help.