Skip to main content
gamedev.net
🔒 Locked

Web programming questions #2

Started by d000hg Mar 4, 2004 at 5:03 AM 13 replies 500+ views
Original Post
d000hg
d000hg
I've pretty much solved this now, don't read it unless out of interest! Thanks all. How can I pass a variable to a page without using server scripting? Like where you do url?arg, except I just want to use client side scripting ie JavaScript. Secondly how can I store the value a variable is given through refreshinbg a page? eg I have a var t=3 in a script in the head of the page, then later change it to 4 with some function. I refresh the page to display differently based on t's new value but it reverts to 3. Am I forced into cookies? [edited by - d000hg on March 5, 2004 7:35:01 AM]
bgreen
bgreen
Well, HTTP is stateless, in that the current web page you are view knows nothing about where you have been, or what variables you have set (say in ASP).

I know of no other way to pass variables, and I don''t think there is a way to do this (someone correct me if I''m wrong).

As with the cookies, you could do that, or save values into a database on the server side using some form of sessions - both ASP and PHP support sessions.
Jolle
Jolle
Yeah, I too think you will have to use cookies for storing your stuff.

About the passing args, you can do that, but I think you will have to do the parsing of it yourself. Here's a function I've written for it (and it works, I use it on a page).

function ReadVars()
{
var p = location.href.indexOf("?") + 1;
if (p > 0)
{
var vars = location.href.slice(p).split("&");
for (var i = 0; i < vars.length; i++)
eval(vars[ i ]);
}
}


so if it gets called like index.html?gnu=1&i=5, the functino ReadVars sets the global variables gnu and i to gnu=1 and i=5.

edit: brackets go buggered

[edited by - Jolle on March 4, 2004 6:29:01 AM]
neoztar
neoztar
I''ve seen it done by appending all variables as a anchor target at the end of the url, like yourpage.html#t=3 and then getting the address and parsing it to get the variables back again.

~nz

// Website // Google // GameDev // NeHe // MSDN // OpenGL Extensions //
d000hg
d000hg
Jolle:

That''s really useful, hope you don''t mind if I use it?! I could use the same technique for storing a couple of variables, by reloading the page with the variables appended to the url.

It just seems dumb to have to use server scripting to build all your page again when you want to change one small part of it.
Jolle
Jolle
Sure, you can use it
lemurion
lemurion
quote:
Original post by d000hg
It just seems dumb to have to use server scripting to build all your page again when you want to change one small part of it.


you can use pure HTML with php:
ex:


<HTML>
<HEAD>
<TITLE>Hello world from php
</HEAD>
<BODY>
<H1><?php echo "Hello world from php !"; ?></H1>
</body>
</html>


all you have to do is put the php code inside <?php and ?>

Matt

[edited by - lemurion on March 4, 2004 8:05:59 AM]

[edited by - lemurion on March 4, 2004 8:06:29 AM]
Matt
d000hg
d000hg
But that''s done on the server and has to be rebuilt every time you change something. If you have many users it''s a lot of load on the server surely?
OrangyTang
OrangyTang
Unless you''re running on a P90, or you''ve getting slashdot volumes of traffic, I wouldn''t worry about it. The bottleneck will probably be actual data transmission, not page generation (unless you have some crazy complicated stuff going on).

Try it the simple way. If its too slow (and you''re certain that its a problem) then you can change it.
d000hg
d000hg
The speed isn''t an issue but I''m doing this to impress not for my own amusement. I''ve got it working now with nothing except Javascript - two frames, the navbar updates according to which file is being viewed in the main frame even if it''s changed without using the navbar. I''m quite pleased that I got this, is it likely to seem good or just pointless to evade PHP/ASP?

Jolle:
slice(), split() & particularly eval() - where can you suggest finding more info on functions like these? Some reference of all standard functions somewhere?
Dark_Nebula
Dark_Nebula
Well, all of this is fine for your average homepage. But if you want to do anything more than that you should really go for a server side solution.

You are not limited to interpreted scripting.
Why would you use ASP when there''s ASP.NET?
CGameProgrammer
CGameProgrammer
I don''t understand what the problem is. If you want to pass variables, you specify them on the command line. Like:

function ViewProfile ( Name )
{
location.href = "profile.php?name=" + Name;
}

It has nothing to do with server-side scripting; rather, this method is called HTTP-GET and it''s an HTTP protocol thing.

Another alternative is with forms.

Another alternative is with cookies or session variables. To make a session variable, you write a cookie without specifying an expiration time (or you set the expire time to 0).

~CGameProgrammer( );

Screenshots of your games or desktop captures -- Upload up to four 1600x1200 screenshots of your projects, registration optional. View all existing ones in the archives..
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
Dark_Nebula
Dark_Nebula
True. That is indeed a HTTP GET request. What happens next is that the server recieves the request, process it and returns a HTTP response. The only way for the client to process the variable(s) sent with the request is to examine the url. Like Jolle described.
Using a form is usually a HTTP POST request (however you can specify it as a GET request). This is all fine for _sending_ variables.

My point is that this is not a very clever way to do things. It's a lot easier to process the requests on the server. It allows for a much more robust architecture. In my opinion, client-side script should only be used to design a GUI, not to design an application architecture.

Cookies can be used as you describe, but the normal thing to do is to store some kind of unique user id in the cookie and then use this id to get state variables from some kind of storage ie. server-side sessions, databases etc.

I don't know what d000hg is trying to do, but when dealing with dynamic web sites, server-side is the way to go.

EDIT: Typo:/

[edited by - dark_nebula on March 5, 2004 6:57:43 AM]
d000hg
d000hg
OK, if server-side is the way it''s done I''ll do iy that way too! I have it working using just javascript but moving the work to ASP would hardly be challenging! (I''m not using ASP.NET because I haven''t got round to learning it yet, or installing the .net framework for that matter!)
So it''s not even interesting that I did it without server scripting then
Jolle
Jolle
quote:
Original post by d000hg Jolle:
slice(), split() & particularly eval() - where can you suggest finding more info on functions like these? Some reference of all standard functions somewhere?


Look at this: http://www.ecma-international.org/publications/standards/Ecma-262.htm. Ecma-262 is (or should be) the standard used by JavaScript & JScript.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.