Welcome to WebmasterWorld Guest from 3.214.184.196

Forum Moderators: open

Message Too Old, No Replies

parsing information from the URL with javascript.

     
12:37 am on Nov 21, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 21, 2003
posts:8
votes: 0


ok. quick and simple.

<script type="text/javascript" src="http://www.urlhere.com/whatever/vote.php?siteid=42"></script>

now. i know i can include javascript like this. BUT i want to get the 42 out of this URL (using javascript) and print it into the document.

how exactly would i do it?

someone said to me document.location.search but didnt tell me how (on another rival forum which i dont really like) so if anyone would be so kind as to show me how... using location.href would be nice :) if that would work :/

i know for a fact you cant use php, because calling the document as a javascript file seems to force it to read the php tags in javascript coming up with all sorts of wierd errors. hehe. anyway, if anyone could help... THANK YOU!

its for this site...

<snip>

and the banner is what the javascript shows, but i want each member site (of my link site) to display a network banner (by choice to add the code) which will have a link with vote.php?siteid=(THERE SITE ID HERE) on the banner.

Thanks again. Luke (horza) Oliff.

[edited by: Woz at 12:49 am (utc) on Nov. 21, 2003]
[edit reason] No URLs Please, TOS#13 [/edit]

12:43 am on Nov 21, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:May 16, 2003
posts:592
votes: 0


Here is a script Ive got kicking around should do the trick for you:


// QueryString
// Call function by x = querystring("variable") returns variable=x

function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.length;i++)
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
var query = window.location.search.substring(1);
var pairs = query.split("&");

for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf('=');
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryString.keys.length] = argname;
QueryString.values[QueryString.values.length] = value;
}
}
}
QueryString_Parse();

BTW you arent allowed to post links to personal sites. Welcome to webmasterworld

1:01 am on Nov 21, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 21, 2003
posts:8
votes: 0


lol sorry, no problem. signed up in a rush didnt read rules. ill browse over them tomorrow morning. its 1am wana get this done quickly.

and thanks for the warm welcome ;)

so all i do is type

QueryString("siteid") and it will take the siteid from the URL and write it into the script?

[edit, i am a complete n00b with javascript]

6:31 am on Nov 21, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Aug 26, 2003
posts:311
votes: 0


Hi,

I'm have a few issues with a similar script.

I am able to parse the URL, then write the value to a cookie, however with the script I have when returning to [widget.com...] it overwrite the value with a null value.

I guess I would need to have the script first see if there is a value in the cookie and then if not take the value from the URL.

I am new to JavaScript (my first day) and am getting a little over my neck. Any help would be appreciated, here is the script I have so far:

function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.length;i++)
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
var query = window.location.search.substring(1);
var pairs = query.split("&");

for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf('=');
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryString.keys.length] = argname;
QueryString.values[QueryString.values.length] = value;
}
}

}

QueryString_Parse();

var expDays = 90;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

document.cookie = "referrer_cookie=" + escape(QueryString("referrer_id")) + "; expires=" + exp.toGMTString() + "; path=/" + "; domain=.WIDGET.COM";

11:51 am on Nov 21, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 21, 2003
posts:8
votes: 0


:o

but still not help for Horza :(

people tell me things but never explain how to use :( or i dont understand how they explained it :(

5:22 pm on Nov 21, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:May 16, 2003
posts:592
votes: 0


Horza,

If your URL is *ttp://www.urlhere.com/whatever/vote.php?siteid=42

and you want the siteid you would do this...

var x;
x = querystring("siteid");

x will now have the value of 42. To display that value on the page you need to use a document.write statement.

Hope that helps.

10:26 pm on Nov 21, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 21, 2003
posts:8
votes: 0


i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you i love you

:D thanks so much.

11:18 pm on Nov 21, 2003 (gmt 0)

Preferred Member

10+ Year Member

joined:May 16, 2003
posts:592
votes: 0


Yay somebody loves me.
11:28 pm on Nov 21, 2003 (gmt 0)

Full Member

10+ Year Member

joined:Aug 26, 2003
posts:311
votes: 0


I solved my script too! Wahey

if (document.cookie && document.cookie.indexOf("referrer_cookie=") > -1)
{
var refer = document.cookie;
}

else{
function QueryString(key)
{
var value = ("");
for (var i=0;i<QueryString.keys.length;i++)
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse()
{
var query = window.location.search.substring(1);
var pairs = query.split("&");

for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf('=');
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryString.keys.length] = argname;
QueryString.values[QueryString.values.length] = value;
}
}
}

QueryString_Parse();

var expDays = 90;
var exp = new Date();
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
document.cookie = "referrer_cookie=" + escape(QueryString("referrer_id")) + "; expires=" + exp.toGMTString() + "; path=/" + "; domain=.WIDGET.COM";
}

2:18 am on Nov 22, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 21, 2003
posts:8
votes: 0


sorry to be a pain in the butt

but how to i document.write it? its got me confused :(

sorry im used to working with php so this javascript is really confusing me

document.write('<a href=\"http://www.gamerforums.org/linksite/vote.php?siteID=\" target=\"_blank\">Vote for this site</a>');

how would i edit this to do it? :(

2:27 am on Nov 22, 2003 (gmt 0)

New User

10+ Year Member

joined:Nov 21, 2003
posts:8
votes: 0


bahhhhhhhhh...

can you just edit this for me.. cuz with the code you gave me, its stopped it from even displaying.


function QueryString(key)
{
var value = null;
for (var i=0;i<QueryString.keys.length;i++)
{
if (QueryString.keys[i]==key)
{
value = QueryString.values[i];
break;
}
}
return value;
}
QueryString.keys = new Array();
QueryString.values = new Array();
function QueryString_Parse()
{
var query = window.location.search.substring(1);
var pairs = query.split("&");
for (var i=0;i<pairs.length;i++)
{
var pos = pairs[i].indexOf('=');
if (pos >= 0)
{
var argname = pairs[i].substring(0,pos);
var value = pairs[i].substring(pos+1);
QueryString.keys[QueryString.keys.length] = argname;
QueryString.values[QueryString.values.length] = value;
}
}
}
QueryString_Parse();

var x;
x = querystring("siteid");

document.write('<div align=\"left\" style=\"position: absolute\; left: 0\; top: 0\; width: 500\; z-index:1\; height:10\">');
document.write('<table width=\"500\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\" height=\"25\" style=\"border-collapse: collapse\" bordercolor=\"#111111\">');
document.write(' <tr>');
document.write(' <td valign=\"top\" height=\"25\" width=\"148\">');
document.write(' <a href=\"http://www.gamerforums.org/linksite/index.php');
document.write(' \" target=\"_blank\"><img border=\"0\" src=\"http://www.gamerforums.org/linksite/network/banner_logo_top.gif\" width=\"148\" height=\"25\"></a></td>');
document.write(' <td align=\"center\" height=\"25\" width=\"329\" background=\"http://www.gamerforums.org/linksite/network/banner_bg.gif\"><font face=\"verdana\" size=\"1\"><a href=\"http://www.gamerforums.org/linksite/vote.php?siteID=\" target=\"_blank\">Vote for this site</a></font>');
document.write(' </td>');
document.write(' <td height=\"25\" width=\"25\">');
document.write(' <img border=\"0\" src=\"http://www.gamerforums.org/linksite/network/banner_corner.gif\" width=\"25\" height=\"25\"></td>');
document.write(' </tr>');
document.write(' <tr>');
document.write(' <td width=\"500\" colspan=\"3\">');
document.write(' <img border=\"0\" src=\"http://www.gamerforums.org/linksite/network/banner_logo_bottom.gif\" width=\"148\" height=\"22\"></td>');
document.write(' </tr>');
document.write('</table>');
document.write('</div>');

:((