Harry
Harry

Reputation: 329

Grab data from address bar and show it on page

I was wondering if there was any way of grabbing a variable from the address bar, and displaying that that variable on a webpage in the form of text.

So, the code of my webpage recognises a call to action in the web address, lets call that call to action: 'show-this-text-on-page'

Then the site reads the variable, lets call that "RANDOM TEXT" and then displays it on my webpage in the form of text.

for example...

User clicks link to my website: http://site.com/?show-this-text-on-page="RANDOM TEXT"

And on my webpage, "RANDOM TEXT" is displayed in the form of text.

I don't know what code this could be done in, i would presume PHP, sorry :/ Thanks for any help.

Upvotes: 1

Views: 1419

Answers (2)

j08691
j08691

Reputation: 207923

Using PHP you can output the text via the $_SERVER['QUERY_STRING'], $_REQUEST['show-this-text-on-page'] or $_GET['show-this-text-on-page']variables.

For example:

<?php
echo $_GET['show-this-text-on-page'];
?>

Would output "RANDOM TEXT"

Upvotes: 3

Quentin
Quentin

Reputation: 943996

Since you tagged this JavaScript, see the location property. You'll need to perform string manipulation to extract the bits you care about.

I don't mind what coding language this is in

As a rule of thumb, server side languages are a better choice (since they don't depend on the client executing your program code). You'll have a hard time finding one which won't let you get at the URI, but the specifics will depend on the language you choose and how it interfaces with the web server.

Upvotes: 5

Related Questions