Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Client Side Scripting
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 09-22-2005, 12:44 AM
  #1
gibby
SuperHero (Level 14)
 
Join Date: Sep 2004
Posts: 263
iTrader: (0)
gibby is on a distinguished road
retrieving variables from URL in javascript

Hello,

Say I've got a test page that I open with the following URL:

http://localhost/test.html?name=Clark

And on the page I've got this javascript code:

<!DOCTYPE HTML "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
function sayhello(name)
{
document.write('hello ' + name + '!');
return;
}
</script>
</head>
<body>
<script language="javascript">
sayhello(name);
</script>
</body>
</html>

But when I run this page with the URL above, I get

hello !

That is, the value of name (Clark) does not show up.

I thought that in Javascript, referencing variables was as simple as using their names, so if you had a variable with a value in the URL, you could use it in your javascript code just as you would if you declared it locally. Is this wrong? Is there a different way of doing it?

You can see this page by type the following URL in the address bar:

http://www.shahspace.com/test.html?name=[yourname]

Thanks for your help.
gibby is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 09-22-2005, 01:18 AM
  #2
maskd
Swordman (Level 9)
 
maskd's Avatar
 
Join Date: Sep 2005
Location: NSW, Australia
Posts: 88
iTrader: (0)
maskd is an unknown quantity at this point
I don't really see it as practical, but it can be done, but it's not as simple as you think.

A simply worded Google Search brought up a few answers, this being the first one:

Code:
<script language="JavaScript">
<!--
// Create variable is_input to see if there is a ? in the url
var is_input = document.URL.indexOf('?');

// Check the position of the ? in the url
if (is_input != -1)
{ 
// Create variable from ? in the url to the end of the string
addr_str = document.URL.substring(is_input+1, document.URL.length);

// Loop through the url and write out values found
// or a line break to seperate values by the &
for (count = 0; count < addr_str.length; count++) 
{

if (addr_str.charAt(count) == "&") 
// Write a line break for each & found
{document.write ("<br>");}

else 
// Write the part of the url 
{document.write (addr_str.charAt(count));}

}}

// If there is no ? in the url state no values found
else
{document.write("No values detected");}

-->
</script>
maskd is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote

Reply
KEEP TABS
SPONSORS
 
Boxedart
 
 


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 01:59 AM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.