Hello DaniWeb members, I've just joined up and I would like to have one of my problems solved. I'm still a begginer at JavaScript coding so my request may seem a be easy for you guys to answer so here it is:

I have a JavaScript code and I want it to have a delay of 4 seconds before starting, the code is:

<SCRIPT LANGUAGE="JavaScript">
var password = ''
password=prompt('Please enter your password:','');
if (password != null) {
location.href= password + ".html";
}
</SCRIPT>

I've searched through many sites and I havn't found an answer that'll help me, so i'm hoping that you guys will. :cheesy:

Thanks - Cronos

Dani AI

Generated

This thread shows the right idea: was correct to suggest delaying the action with a timer, and correctly wanted the prompt to appear after a short pause. A few practical improvements make the behavior safer and more robust today.

Run the delayed work from a local scope (avoid polluting globals), schedule with a function reference rather than a string, and wait until the DOM is ready if your script sits in the page head. Avoid using window.prompt to collect passwords (it is blocking and not masked) and do not embed credentials in filenames or URLs — handle authentication on the server over HTTPS instead. See the browser API notes on timers and on prompts for details: setTimeout docs and prompt docs. For secure handling of authentication, follow server-side guidance such as the OWASP authentication cheat sheet: OWASP Authentication Cheat Sheet.

A simple, modern pattern is to schedule a short, single-shot timer after DOM readiness, validate and sanitize the user input (allow only expected characters), and then navigate with location.assign or location.replace depending on whether you want a history entry. Timers are approximate and may be clamped in background tabs, so do not rely on millisecond precision for critical flows — see the setTimeout docs for clamping behavior.

Recommended Answers

All 3 Replies

Put your script within a function and then use "setTimeout" to run the function:

TimeOut1=setTimeout("YourFunction()",4000);

Sorry, but could you please kindly help me place the script I have into that function? I'm very new at this, so sorry for any inconvenience.:confused:

The name "password" might be a reserved word, so I changed it to "pword" to avoid trouble:

<script type="text/javascript">
  function Password() {
    var password = ''
    pword=prompt('Please enter your password:','');
    if (pword == '') {
      alert('The password was null.');
    } else {
      location.href= pword + ".html";
    }
  }
  TimeOut1=setTimeout("Password()",4000);
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.