Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3996620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:18:05+00:00 2026-05-20T07:18:05+00:00

i came upon this code while studying settimeout. the settimeout executes an alert 5

  • 0

i came upon this code while studying settimeout. the settimeout executes an alert 5 seconds after a button is clicked.

    <input type="button" name="clickMe" value="Click me and wait!" 
onclick="setTimeout('alert(\'Surprise!\')', 1000)">

however, i saw that the alert string inside has a format ive never seen before. the \ occurs before the ‘ Surprise! \’. what is its use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T07:18:05+00:00Added an answer on May 20, 2026 at 7:18 am

    This is the indented command: alert('Surprise!'). It contains two quotes.
    In this case the command is passed as a string to setTimeout.
    This string is delimited by quotes: 'string'.
    Together this would look like 'alert('Surprise!')', which is invalid syntax, because it’s parsed like this:

    'alert('    // string
    Surprise!   // nonsense
    ')'         // another string
    

    So the quotes inside the string are escaped to signify “this is not the end of the string”.

    This is about the worst possible way to do this though. A better way would be to alternate the two available quote types:

    'alert("Surprise!")'
    

    This will mess up in this case though because it will confuse the HTML parser.

    An even better way is to pass an anonymous function instead of a string:

    setTimeout(function () { alert('Surprise!'); }, 1000)
    

    An even betterer way is unobtrusive Javascript, in which you’re not using HTML onclick attributes, but attach the Javascript to a DOM element programmatically:

    <input type="button" name="clickMe" value="Click me and wait!" id="clicker">
    
    <script type="text/javascript" charset="utf-8">
        document.getElementById('clicker').onclick = function () { 
            setTimeout(function () { alert('Surprise!'); }, 1000);
        };
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While doing some small regex task I came upon this problem. I have a
While reading proggit today, I came upon this comment in a post about how
Ok, I haven't programmed in C# before but I came upon this code and
Hey, I just started wondering about this as I came upon a code that
This question is about 64-bit Java apps in general, although I came upon it
While looking for a light-weight Scala development environment, I came upon an Scala edit
I was recently looking at some vp8 sample decoder code when I came upon
This is an error that came up after I got a previous question here
I recently came upon some old code which I didn't write. It uses WWW::Facebook::API
This question originated when I came upon ( another thread ) about Python's datetime

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.