SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    document.createElement("script") works sometimes in FF2

    Hello all,

    I am having an issue with an email registration script that I have. I worked fine in all browsers until recently FF2 started only working occasionally (at least I think that's the case). Anyway the code below works in FF3 IE7 & 8 but only works some times in FF2. Also it seems to work in FF2 when I threw in an alert() for testing. Any idea what could be going wrong. I have removed my site name for obvious reasons. Thanks for any help!

    Code:
    function validateFields()
    {
      var email = document.forms[0].elements["EMAILADDR_TopSlide"];
      var rpsNwsMessageTag = document.getElementById("rpsTopNwsMessage");
      var ecodes = "5555";
        
      if (email.value != "" && email.value != "Enter Email Address")
      {
        if (!/^[\w\.\-]+@([\w\-]+\.)+[a-zA-Z]+$/.test(email.value) || email.value == "" || email.value == null) {
            rpsNwsMessageTag.innerHTML = "<img alt='left' src='rollout_widget_lefticon.gif' /><span style='color: #FFC'>Please enter a valid email address</span>";
            return false;
        }
        email = encodeURIComponent(email.value);
        ecodes = encodeURIComponent(ecodes);registration
    
        var url = "http://www.******.com/********.html?requestType=NewsletterSubscription&EMAILADDR=" + email + "&options=" + ecodes;
    
        var scriptTag = document.createElement("script");
        scriptTag.type = "text/javascript";
        scriptTag.src = url;
    
        document.body.appendChild(scriptTag);
        /*alert(scriptTag)*/
        document.cookie = "*******";
    
        history.go()
        return true;
      }
      else
      {
        rpsNwsMessageTag.innerHTML = "<img alt='left' src='rollout_widget_lefticon.gif' /><span style='color: #FFC'>Please enter a valid email address</span>";
        return false;
      }
    }

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The source file probably does not have time to load and do whatever is expected of it, before you attempt to set a cookie and reload the page, after which incidentally a return statement is pointless since it won't execute.

    It may help to explain what you're trying to do.
    Tab-indentation is a crime against humanity.

  3. #3
    SitePoint Zealot matches's Avatar
    Join Date
    Aug 2006
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The source file probably does not have time to load
    Yeah, I am trying right to try to implement setTimeout to "stall" the script for a moment. Haven't had any luck yet.

    It may help to explain what you're trying to do.
    It's just a newsletter registration form. When the email is entered it get sent that url. After that I am not really sure what happens. That's legacy stuff that nobody in my office seems to have an answer to.

    Do you think setTimeout might work well to give the script enough time to run?

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2007
    Posts
    345
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think setTimeout will make any difference if it's within the same function. It won't fire until the current function has finished executing, even if you set the delay to 0. Also, there's no guarantee how long the script will take to load or even if it will load.

    Script elements have a load event.
    Code:
    scriptTag.onload = function(){
        // do stuff once the script has loaded
    }
    scriptTag.type = "text/javascript";
    scriptTag.src = url;
    
    document.body.appendChild(scriptTag);


Bookmarks

Posting Permissions

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