...

View Full Version : How do I press a button with Javascript, but stop it after so many clicks?



DeadlyFishy
06-25-2009, 05:38 PM
I'm really new to Javascript, and really know basically nothing about it. What I want to do is press a skip button on a page, then wait for the next page to load completely before pressing the skip button. I want the script to stop after the first five skips, though.

I plan on making this into a GreaseMonkey script for my own personal use, if that info is needed, too.

Philip M
06-25-2009, 06:12 PM
If each page is a separate url and each "skip" button is created by that page, then you will have to set a cookie to count the total number of clicks so that the value is passed from page to page. But why not simply make each click of the button redirect to the next page, and omit the button from the final page?

If each new page is loaded into an iframe you will want something like


<input type = "button" value = "Skip a Page" onclick = "skip()">

<script type = "text/javascript">
var count = 0;
function skip() {
count ++;
if (count == 1) {window.location = "page1.html"}
if (count == 2) {window.location = "page2.html"}
if (count == 3) {window.location = "page3.html"}
if (count == 4) {window.location = "page4.html"}
if (count >= 5) {window.location = "page5.html"}
}
</script>


Quizmaster: When commuters are squashed together on trains, they are said to be packed in like what?
Contestant: Eels.

DeadlyFishy
06-25-2009, 07:09 PM
Unfortunately, the pages do not have separate URLs. They appear to be something like frames, but I don't think they really are. Does "dynamic flow" mean anything to you?

An example of a page I would use this code on is here: https://www.eversave.com/common/dynamicflows/DynamicPollFlow.jsp

The page will probably not open unless you're signed in, but the ".jsp" file name can maybe give you an idea of what you're supposed to be looking at.

Philip M
06-25-2009, 07:59 PM
Does "dynamic flow" mean anything to you?


Sure - it is something to do with yoga, isn't it? :D

DeadlyFishy
06-25-2009, 08:35 PM
Sure - it is something to do with yoga, isn't it? :D

Well that's no help, but at least I got a laugh! :thumbsup:

DeadlyFishy
06-26-2009, 11:20 PM
The original Just Say No! GreaseMonkey script is here..



inputs = document.getElementsByTagName("input");
var checkedyes = 0;
var radios=0;
var firstradio=0;
var foundradio=false;
var i=0;
for(i=0;i<inputs.length;i++)
{
if(inputs[i].type=="radio")
{
if(!foundradio)
{
firstradio = i;
foundradio = true;
}
radios++;
if(inputs[i].value.toLowerCase()=="no")
{
inputs[i].checked = true;
inputs[i].click();
document.body.focus();
}
else
{
inputs[i].checked=true;
}
}

}
if (radios>2)
{
inputs[firstradio+2].checked=true;
}

next = document.getElementById("nextOffer");
if (next == null)
{
next = document.getElementById("pass");
}
if (next == null)
{
next = document.getElementById("bt_cancel");
}
if(next == null)
{
for(i=0;i<inputs.length;i++)
{
if((inputs[i].type=="submit") && (inputs[i].value.toLowerCase()=="skip"))
{
next = inputs[i];
break;
}
}
}
if(next==null)
{
for(i=0;i<inputs.length;i++)
{
if((inputs[i].value.toLowerCase()=="submit") || (inputs[i].name.toLowerCase()=="submit"))
{
next = inputs[i];
break;
}
}
}
if(next == null)
{
next = document.getElementById("submitbutton");
}
next.focus();
next.click();


That seems to work a majority of the time for the skipping, though I think some buttons are named differently or something because it doesn't catch them all. Is there a way to modify this code so it will stop after five skips?

Also, found this in the pages source where I want the skipping to happen


<input type="image" alt="Skip" name="bt_cancel" id="bt_cancel" src="https://images.eversave.com/Images/newsletter/QzGreyskipbtn051408.gif" onClick="return handleSubmit(this);"/>

That appears to be a skip button, if that helps at all.

Philip M
06-27-2009, 08:24 AM
onClick="return handleSubmit(this);"

I don't see a function handleSubmit() but you could quite easily do something like


var skipCount = 0;
function handleSubmit() {
skipCount ++;
if (skipCount > 5) {return false}

DeadlyFishy
06-29-2009, 12:39 AM
The code snippet that I said appears to be a skip button was from the page source, not from the GreaseMonkey script or anything. I'm still sort of confused. Do you think there's anyways you could just modify the GreaseMonkey script to make it stop after five skips?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum