Could this be done in javascript?

D

Diddum

Hello everybody.
Until now I've only needed some php for my simple web pages, but now
I think I may need to learn javascript. Before that, I would like to
know
if the following behaviour can be obtained by means of some javascript
code

Simplifying... I have a web page (say mymain.php) in php/html with a
html form,
with just a text field and a button. Something like this:

<form method="POST" action="foobar.php" >
<input type="text" name="footxt" SIZE="8">
<input type="submit" value="gogogo">
</form>

This is the scenario that I want to change:
The user lands on this page (mymain.php), then put something in the
text field,
then clicks on the button, invoking the action page foobar.php.
Then usually the user go back (with "back" on the browser) to
mymain.php,
change the field, click the button, and so invokes again foobar.php.
And so on.
In practice, the page mymain.php is loaded once (then lies in the
browser memory)
while foobar.php is loaded with POST parameters each time the user
clicks the button.

What I want to obtain:
When the user clicks on the button, instead of loading always
foobar.php,
I want that the form invokes randomly, say, fub1.php and fub2.php.

Clearly, using server side php, I can modify easily mymain.php in such
a way that
more or less half of the users load a form with action="fub1.php" and
the
others load a page with action="fub2.php". But in this way, once
mymain.php
has been loaded, each user, going back and forth, will load always
fub1.php
or always fub2.php.
Instead I want that the same form, in some way, would lead sometimes
to fub1.php and sometimes to fub2.php.
Like, magically, the click on the button could randomly rewrite the
action target of the
form....

I apologize for this long message, but while the problem is simple, I
had some
difficulties in explaining it clearly.

thanks,
g.
 
D

Diddum

Don't worry, I studied javascript for 5 minutes and I found the
solution!

Great language!

bye,
g.
 
D

Diddum

Just out of curiosity, why do you want to do this?

It is quite stupid.
This is why.

I have a page that performs, say, a sort of computation given an input
in a form.

In the result page I display some Google AdSense ads.

The ads of Google Adsense are sensitive to the page content. So, for
example,
if you display a page with some comments on Obama you will get ads
referring to politics.

Since my page displays the results of a computation the ads displayed
have
little content to work on and, basically, suck.

So I wanted to add some content to my result page. I do not want to
cheat adsense,
but clearly my page is a little out of this contextual-ads game. So I
want to
add some related and relevant material to the result page. More that
this, I want to experiment
different related materials in such a way that
1) adsense can associate a content to a specific web address
(like foobar1.php is a page related to politics and foobar2.php is
related to mathematics)
2) a user that repeatedly click the button on the form is going to see
both foobar1.php and
foobar2.php pages. In this way (s)he will be exposed to more varied
ads.

By the ways, this is more of less a pastime to force myself to learn
some
php, css, and hopefully some javascript, since my earnings with ads
amount to less than
2 daily bucks...

Probably there is a more clever way to do this, but I simply
created a function named randaction() which get a handle on the form
and randomly modify the action field. Then I added a
onClick="randaction()" to my
input form.

This seems to works. In practice the onclick seems to change on-the-
fly the action field
of my form just before it is used to jump away.

bye,
g.
 
S

Scott Sauyet

What I want to obtain:
When the user clicks on the button, instead of loading always
foobar.php,
I want that the form invokes randomly, say, fub1.php and fub2.php.

Do you want to consider users who do not have Javascript available and
on at your site? They're a pretty small minority, but they certainly
exist. If you did this on the server, it work for everyone.

This is a forum about JS, and I'm glad you found a JS solution, but it
should be pretty simple to do in PHP:

<?php
if (rand(0, 1) == 1) {
include("fub1.php");
} else {
include("fub2.php");
}
?>

Good luck,

-- Scott
 
D

David Mark

Scott said:
Do you want to consider users who do not have Javascript available and
on at your site? They're a pretty small minority, but they certainly
exist.

What's the most popular add-on for FF? I've heard that it is Noscript
of late. Nor surprising considering how poorly most documents are
scripted (the result of three years of jQuery mass hysteria).

Then there are those pesky blind people, corporate users with
overzealous network admins, people using Blackberry browsers who don't
know or care to enable JS, etc., etc. There is no point in thinking of
this "group" as a small minority. It just opens the door for slovenly
development.
If you did this on the server, it work for everyone.

Right. No need to exclude users without cause. The first excuse I
usually hear is "we didn't have time". Of course, that's backwards.
They had time to foul things up, didn't they?
 
D

Diddum

David Mark wrote:
Scott said:
If you did this on the server, it work for everyone.

Thanks. That is what I actually already do.
As I explained (poorly, it appears) is that in the solution above
the random choice is made once at server side and so it is somehow
"frozen" in the document. If a user uses the same form multiple times
hitting the "back" button on the browser, the form will always invoke
either fub1.php or fub2.php, not a mix of the two as I wanted.

Now, I use both techniques, that is I use php to set the first value
of "action" randomly. Then, I use onSubmit in the form to force a
random
change of the action value when the user tries to submit the form.
In this way, if javascript is not turned on, about half of the users
will repeatedly invoke fub1.php and other half will invoke fub2.php,
which
is subpar but OK.
If javascript is enabled, the single user, by using the form several
times,
will probably see both pages (with high probability).

bye,
g.
 
J

Jorge

What's the most popular add-on for FF?  I've heard that it is Noscript
of late.  Nor surprising considering how poorly most documents are
scripted (the result of three years of jQuery mass hysteria).

Then there are those pesky blind people, corporate users with
overzealous network admins, people using Blackberry browsers who don't
know or care to enable JS, etc., etc.  There is no point in thinking of
this "group" as a small minority.  It just opens the door for slovenly
development.

Let's put this heading in the FAQ:
<h1>"Many regulars in this group would like you to note -in the first
place- that this silly thing called JavaScript ought to be turned off
in your browser, to begin with."</h1>
 
S

Scott Sauyet

Let's put this heading in the FAQ:
<h1>"Many regulars in this group would like you to note -in the first
place- that this silly thing called JavaScript ought to be turned off
in your browser, to begin with."</h1>

Good idea, but I'd suggest we phrase it a little differently:

"Many regulars in this group would like you to note that there are a
number of users who are not willing or not able to use Javascript, so
it's a good idea to build your documents in a manner that allows
reasonable access to your content without Javascript enabled."

-- Scott
 
J

Jorge

"Many regulars in this group would like you to note that there are a
number of users who are not willing or not able to use Javascript, so
it's a good idea to build your documents in a manner that allows
reasonable access to your content without Javascript enabled."

Once you've crossed a certain line there's no way to achieve
"reasonable access" anymore and then the "this site requires
JavaScript" message becomes appropriate. And crossing it is not a sin,
as some regulars will try to make you believe.
 
J

Jorge

BTW, for those "not willing", that's their problem. They could as well
turn CSS off. Or even the network connection for that matter :)

Very very few, but yes, there may be someone. Almost every normal
browser has come with JS since 1995/6, IIRC.
Once you've crossed a certain line there's no way to achieve
"reasonable access" anymore and then the "this site requires
JavaScript" message becomes appropriate. And crossing it is not a sin,
as some regulars will try to make you believe.

Furthermore, no one surfing the web in a browser with these essential
features turned off *expects* it to behave as if they were on. For
example, iPhone users don't expect sites based on the proprietary
Flash® API to work... until yesterday (?) http://www.google.com/search?q=flash+gordon+runtime
)
 
J

Jeremy J Starcher

Once you've crossed a certain line there's no way to achieve "reasonable
access" anymore and then the "this site requires JavaScript" message
becomes appropriate. And crossing it is not a sin, as some regulars will
try to make you believe.


Agreed.

I have a few projects that require Javascript .. including an image map
editor, a compiler and a data entry front-end for a LOCAL database.

My choices were:
1) Write then as a web application in Javascript and have them available
to whoever wanted to use them.

2) Write them as a stand alone application in Linux and -greatly- reduce
their availability

3) Break down and learn (re-learn?) Java and create a stand alone
application.

Option (1) was the only one that made sense. Yes, there are some people
who are unable to use the image map editor. But it mostly[1] excludes
the same category of people who couldn't use my web app.


[1] Yes, there may be a few people who are able/willing to download and
run random executables who also have Javascript turned off, but I'm
willing to wager that is a /very/ small minority.
 
J

Jorge

(...)
3) Break down and learn (re-learn?) Java and create a stand alone
application.
(...)

Java has had plenty of time to prove unequivocally not to be up to its
promised write once run anywhere. That probably isn't solely Sun's
fault, btw, but that's a matter for c.l.java. Instead, JS in a browser
is the ubiquitous VM that might replace it :)
 
J

JR

Java has had plenty of time to prove unequivocally not to be up to its
promised write once run anywhere. That probably isn't solely Sun's
fault, btw, but that's a matter for c.l.java. Instead, JS in a browser
is the ubiquitous VM that might replace it :)

Python is the best alternative to Java.
 
J

JR

Hello everybody.
Until now I've only needed some php for my simple web pages, but now
I think I may need to learn javascript. Before that, I would like to
know
if the following behaviour can be obtained by means of some javascript
code

Simplifying... I have a web page (say mymain.php) in php/html with a
html form,
with just a text field and a button. Something like this:

<form  method="POST"  action="foobar.php" >
<input type="text"  name="footxt" SIZE="8">
<input type="submit" value="gogogo">
</form>

This is the scenario that I want to change:
The user lands on this page (mymain.php), then put something in the
text field,
then clicks on the button, invoking the action page foobar.php.
Then usually the user go back (with "back" on the browser) to
mymain.php,
change the field, click the button, and so invokes again foobar.php.
And so on.
In practice, the page mymain.php is loaded once (then lies in the
browser memory)
while foobar.php is loaded with POST parameters each time the user
clicks the button.

What I want to obtain:
When the user clicks on the button, instead of loading always
foobar.php,
I want that the form invokes randomly, say, fub1.php and fub2.php.

Clearly, using server side php, I can modify easily mymain.php in such
a way that
more or less half of the users load a form with action="fub1.php" and
the
others load a page with action="fub2.php". But in this way, once
mymain.php
has been loaded, each user, going back and forth, will load always
fub1.php
or always fub2.php.
Instead I want that the same form, in some way, would lead sometimes
to fub1.php and sometimes to fub2.php.
Like, magically, the click on the button could randomly rewrite the
action target of the
form....

I apologize for this long message, but while the problem is simple, I
had some
difficulties in explaining it clearly.

Have you tried using headers (Expires and Cache-Control) instead of
Javascript?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top