www.webdeveloper.com
Results 1 to 6 of 6

Thread: Why doesn't window.open work in this simple email validation script?

  1. #1
    Join Date
    Mar 2013
    Posts
    24

    Question Why doesn't window.open work in this simple email validation script?

    I have a simple email validation script. After email is validated the script writes a cookie, then opens a new window.

    For the life of me I cannot figure out what why window.open will not work. (It works when I remove the cookie setting code. But not with it???).

    What am I missing here?

    Code:
    function emailCheck(emailStr) {
    // checks if the e-mail address is valid
    var emailPat = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var matchArray = emailStr.match(emailPat);
    if (matchArray == null) {
    alert("Your email address seems incorrect.  Please try again (check the '@' and '.'s in the email address)");
    return false;
    }
    // make sure the IP address domain is valid
    var IPArray = matchArray[2].match(/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/);
    if (IPArray != null) {
    for (var i=1;i<=4;i++) {
    if (IPArray[i]>255) {
    alert("Destination IP address is invalid!")
    return false;
          }
       }
    }	
    
    
    
    var email1 = document.email_capture.email.value;
    	var expires = new Date();
    expires.setDate(expires.getDate() + 1825); // 1825 = days to expire	
    	document.cookie = "email2= "+ escape(email1)+"; expires="+ expires.toGMTString()+"; path=/; domain=localhost";
    	
    
    window.open("http://www.testing.com", '_new','fullscreen=yes, scrollbars=auto');	
    
    	
    
    return true;
    }

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    When that function returns true, are you by any chance submitting a form?

    If so then you are dismissing the document and all bets are off regarding what will and won't happen at that point.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Mar 2013
    Posts
    24
    The javascript is triggered on an form onsubmit event. Yes the form is submitted.

    Everything works, the cookie sets properly etc. WHEN the window.open statement is below the cookie code. When it is above the cookie code the window opens, but the cookie isn't set.

    What would I need to change for the cookie to write, and the redirect to happen once the email passes validation?

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Opening a window is not a good way to perform a redirect.
    It should be handled by the form's target page.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  5. #5
    Join Date
    Mar 2013
    Posts
    24
    Understood. But there are several technical reasons why the form submissioin content needs to open in a new window, and needs to be a full size window.

    The form redirect will not give us the function we need. We need a new window to open full size on form submission.

  6. #6
    Join Date
    Mar 2013
    Posts
    24
    UPDATE:

    After a crap load of testing, I've discovered that the script works perfectly on a static HTML page. The error is only showing itself when the Javascript is run on a wordpress page.

    Any ideas why?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles