简体   繁体   中英

Javascript not triggered when supposed to

How come the Javascript isValidEmailAndEqual isn't triggered when submitting the form:

echo("<script language='javascript' type='text/javascript'>
<!--

function isValidEmailAndEqual() {
regExp = /^[^@]+@[^@]+$/;


if((document.subscribe.email1.value.search(regExp) == -1) || !isEqual(document.subscribe.email1.value, document.subscribe.email2.value)) 
   { 
   //alert(isEqual(document.subscribe.email1.value, document.subscribe.email2.value))
       alert('Incorrect entered email addresses. They must be valid e-mail addresses and equal.');
      return false; 
   } 

   //document.subscribeForm.submit();

return true; 
}


-->
</script>");

echo ("<div class='subscribe'>
     <h4>Subscribe</h4>
     <p>Subscribe to iAndApp's newsletter in order to get information about new and updated iPhone games and iPhone applications, that has been released by iAndApp. </p>
     <form action='subscription.php' name='subscribe' method='post'  onsubmit='return isValidEmailAndEqual()'>
        <p class='formlabel'>Förnamn</p> <input type='text' name='first_name'/><br/>
        <p class='formlabel'>Efternamn</p> <input type='text' name='surname'/> <br/>
        <p class='formlabel'>E-mail</p> <input type='text' name='email1'/><br/>
        <p class='formlabel'>Repeat e-mail</p> <input type='text' name='email2'/> <br/>
        <input class='inputsubmit' type='submit' value='Subscribe'/>
     </form>
</div>");

Firebug tells me there is no javascript on the page.

I have tried both with and without comments and prefix the closing comment tag.

What I do, which may matter, is loading the below content from a server and than adding to a div-tag. The content is presented, but still the scripts doesnt' work. I have also tried to include the scripts on the main/parent-page (the page loading content and adding it to a div) where I have other scripts that works (triggered by component in the main/parent page though).

Thank you in advance!

It should be
echo("<script language='javascript' type='text/javascript'>
<!--
//your code here, obviously not commented
//-->
</script>

FYI, http://www.w3.org/TR/html4/interact/scripts.html#h-18.3.2

Actually, they are triggered. The following HTML (exactly as outputted by your script) causes the function to trigger in both firefox 5 and chrome. As a sidenote, you probably do not need the html comments ( <!-- and --> ). That's an practice that is generally no longer necessary since browsers that do not support javascript simply ignore everything within the tags.

That being said if you must have the html comments, comment out the --> to avoid it triggering a JS error (as Kumar explains).

<script language='javascript' type='text/javascript'> 
<!--

function isValidEmailAndEqual() {
regExp = /^[^@]+@[^@]+$/;


if((document.subscribe.email1.value.search(regExp) == -1) || !isEqual(document.subscribe.email1.value, document.subscribe.email2.value)) 
   { 
   //alert(isEqual(document.subscribe.email1.value, document.subscribe.email2.value))
       alert('Incorrect entered email addresses. They must be valid e-mail addresses and equal.');
      return false; 
   } 

   //document.subscribeForm.submit();

return true; 
}


-->
</script><div class='subscribe'> 
     <h4>Subscribe</h4> 
     <p>Subscribe to iAndApp's newsletter in order to get information about new and updated iPhone games and iPhone applications, that has been released by iAndApp. </p> 
     <form action='subscription.php' name='subscribe' method='post'  onsubmit='return isValidEmailAndEqual()'> 
        <p class='formlabel'>Förnamn</p> <input type='text' name='first_name'/><br/> 
        <p class='formlabel'>Efternamn</p> <input type='text' name='surname'/> <br/> 
        <p class='formlabel'>E-mail</p> <input type='text' name='email1'/><br/> 
        <p class='formlabel'>Repeat e-mail</p> <input type='text' name='email2'/> <br/> 
        <input class='inputsubmit' type='submit' value='Subscribe'/> 
     </form> 
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM