开发者

JavaScript does not execute

So in my head> I have the following piece of code:

function dynamicPhoneAddon(){
while(true){
    var phone = document.myform.phone.value;
    document.getElementById('writeme').innerHTML ='<input type="hidden" name="retURL" value="http://URLR开发者_JAVA技巧EMOVED/thankyou.php?customer=' + phone + '">';
    }
}

Then I have a body onload="dynamicPhoneAddon()" to call this script. It's supposed to constantly modify the contents of a div id="writeme" by using the value from the phone field of form myform.

However, the script doesn't run. I haven't done any work with javascript before, so I think it's probably something obvious... is it because I'm calling an infinite loop? Or am I trying to do something impossible?

The end result I'm trying to achieve is to make a hidden value that has a link to a URL with a variable (the phone number) at the end.


Your javascript does execute - forever. Your function never returns because you while loop is running forever.


Instead of that (which will cause some problems), you could get rid of the while loop and have the phone input tag simply have a onchange="dynamicPhoneAddon()"


Your script enters an infinite loop because of the while(true). Since Javascript is single threaded (and cannot run things in parallel) this will take the CPU for itself and not giva a chance for anything else to run.

What you can do to monitor when a user types on the text field is listen to events like onkeypress:

document.myForm.phone.onkeypress = function(evt){
    //do your div updating code here
};

(BTW, why are you using document.myform? Using getElementById is usually more cross-browser)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜