Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Ether Frenzy
Dec 22, 2006




Nap Ghost
I'm having trouble amending this javascript form submission to also allow the "Enter" key to submit the form. I'm not that experienced with javascript, so my googling has not been resulting in any successes at inserting the additional elements.

my form currently checks a text area entry against a list of promotional codes before passing the user along to the next page, what I need it to do is to accept both pressing the 'go' button as well as hitting enter to submit the promotional code, so if anyone could explain to me how to do that I'd really appreciate it.

My form:
code:
<div class="action">
                <form id="codeentry" name="codeentry" action="">
                        
                <input type="text" name="refcode" id="refcode" value=""> <br><br>
               
           <p class="submit"> <input type="button" onclick="refcodecheck()" value=""></p>

</form><br>
                
            </div>
My javascript code checker:
code:
<script language="javascript" type="text/javascript">
    function refcodecheck()

    {   

        var codes = new Array("7 one nine","7-one-nine","7-one nine");

        for (var i=0;i<19;i++)

        {

            if (document.getElementById('refcode').value.toLowerCase() == codes[i])

            {   //code matched

               
                window.location = 'http://www.google.com';

                return false;

            }

        }

        //code did not match

        alert('You have entered an invalid code.  Please try again or go to [url]www.coderequest.com[/url] to register for a valid code.');
      
    }      

</script>  

Adbot
ADBOT LOVES YOU

Ether Frenzy
Dec 22, 2006




Nap Ghost
Thanks for that tutorial link - didn't actually get my problem solved but having looked over that site some more, I'm learning more about JS now than I...uh, really wanted to know.

But I did eventually get my script tweaked using googled results that plugged into my existing form validator...

Here's what did the trick:


code:
  <script language="javascript">
    
function catchEnter(e)

{

    // Catch IE’s window.event if the
    // ‘e’ variable is null.
    // FireFox and others populate the

    // e variable automagically.
   if (!e) e = window.event; 

   // Catch the keyCode into a variable. 
   // IE = keyCode, DOM = which.
   var code = (e.keyCode) ? e.keyCode : e.which;

           

    // If code = 13 (enter) or 3 (return),
    // cancel it out; else keep going and
    // process the key.
    if (code == 13 || code == 3) 
    {
        refcodecheck();
        return false;
    }
    
}


// Anonymous method to push the onkeypress
// onto the document.
// You could finegrain this by
// document.formName.onkeypress or even on a control.
window.onload = function() { document.onkeypress = catchEnter; };


</script>


    <script language="javascript" type="text/javascript">
    function refcodecheck()
    {   

        var codes = new Array("7 one nine","7-one-nine","7-one nine");

        for (var i=0;i<19;i++)

        {

            if (document.getElementById('refcode').value.toLowerCase() == codes[i])

            {   //code matched

               
                window.location = 'http://www.google.com';

                return false;

            }

        }

        //code did not match

        alert('You have entered an invalid code.  Please try again or go to [url]www.coderequest.com[/url] to register for a valid code.');

        

    }      
</script>   

:cheers:

Ether Frenzy
Dec 22, 2006




Nap Ghost

Avenging Dentist posted:

A-are you seriously doing validation on the client side? :barf:

Tell me about it.

I wanted to do serverside validation with PHP but the location of the program is outside our boundaries, and the clients (and my bosses) were happy with the total lack of security. I let them know that this was not the real way to do this, but they didn't care. Which is why I ended up hacking up some JS to function here.

The promo codes are freebie giveaways anyway, that are all virtually the same, but we wanted the illusion that they're original. Fundamentally security wasn't that high a priority with the sponsors, they were more interested that the users are passed through to the part where they spend money.

Kilson posted:

Shouldn't it work just to make your input button type="submit"?

You can't do a satisfactory image replacement of the submit button (or at least not have it work in IE 6, which we stupidly continue to support - would typically be done with CSS v2) with input type="submit", it has to be input type="button" to use an image as the button.

Which generated all this additional nonsense anyway.

Ether Frenzy
Dec 22, 2006




Nap Ghost
I have a question about setting cookies - does anyone know if it's even possible to cookie a user so that he's rendered a different domain's site in a no-mobile (desktop) state?

Like, for instance - set the cookie on page load on a http://adserver.company.com location that links users to http://www.company.com but tell them to only be served the desktop site when they get there, regardless of device? (our mobile team shat the bed and forgot a pretty major feature in their product, so in the rush to not play the blame game they're dumping all the work on my group who doesn't normally write cookies.)

I can't seem to find googled consensus that you can go cross-domain like this reliably, and being pretty unfamiliar with cookies I'm not even sure where to start attempting and I'd rather skip the fun of trying if it's not even doable.

*I am not 100% sure that http://adserver.company.com is a fully a 'subdomain' of http://www.company.com, so that further muddies my searching.*

This is what I'm using and it's working on my http://adserver.company.com pages, showing up as set with the correct value and expiry date, but as soon as the user moves on to the http://www.company.com pages, it's lost (and they get the broken mobile site.)

code:
<script>
function setCookie()
{
var d = new Date();
d.setTime(d.getTime()+(30*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = "nomobile=1; "+expires;
}
</script>
So if anyone who actually knows what they're doing can show me what to add (if this action is even possible) I'd be super grateful.

Ether Frenzy
Dec 22, 2006




Nap Ghost

fletcher posted:

adserver.company.com and https://www.company.com are both subdomains of company.com so you don't have to worry about cross-domain anything (which is good because cross-domain cookies do no exist).

Take a look at the domain attribute of the cookie string. You'll want to specify domain=.company.com so the cookie can be used by all subdomains of company.com.

Thanks... read through that and it made sense but I'm still not having any luck making the domain definition work correctly, I still haven't ruled out the fact that our redirect process is dropping out the cookie even if I DO have it set correctly here. F'n mobile team.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply