6 Replies - 1141 Views - Last Post: 11 March 2013 - 12:52 PM

#1 9011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-March 13

javascript problems

Posted 07 March 2013 - 03:39 AM

Using Redirect Function....
<!DOCTYPE html>
<html>
<head>
<title>Using Redirect function</title>
<script type="text/javascript">
<!--
function delayIt(){
    window.location = "http://www.google.com";
}
//-->
</script>
</head>

<body onload="setTimeout('delayIt()', 50000)">
<h2>Prepare to be redirected!</h2>
<p>You will be redirected to the DBS site!</p>

</body>
</html>



Reacting to Events....
<!DOCTYPE html>
<html>
<body>

<h1>My first Javascript</h1>

<p>
Javascript can react to events. Like the click of a button:
</p>

<button type = "button" onclick = "alert ('Welcome!')">Click Me!</button>

</body>
</html> 



I would like to ask if i can combine both things together in one page,
can anyone teach me how to do it??
thank you very much!

Is This A Good Question/Topic? 0
  • +

Replies To: javascript problems

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,417
  • Joined: 12-December 12

Re: javascript problems

Posted 07 March 2013 - 03:45 AM

You can just copy the button into your first page, but the user would have to click the button within 50 seconds.
Was This Post Helpful? 0
  • +
  • -

#3 9011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-March 13

Re: javascript problems

Posted 07 March 2013 - 04:41 AM

View Postandrewsw, on 07 March 2013 - 03:45 AM, said:

You can just copy the button into your first page, but the user would have to click the button within 50 seconds.


is it that mean i have to create a new page and paste that to the new page??
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3609
  • View blog
  • Posts: 12,417
  • Joined: 12-December 12

Re: javascript problems

Posted 07 March 2013 - 04:47 AM

View Post9011, on 07 March 2013 - 04:41 AM, said:

View Postandrewsw, on 07 March 2013 - 03:45 AM, said:

You can just copy the button into your first page, but the user would have to click the button within 50 seconds.


is it that mean i have to create a new page and paste that to the new page??

What? It is up to you. You could either copy the button into your first page, or create an entirely new page(?). I don't really understand your question.
Was This Post Helpful? 0
  • +
  • -

#5 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: javascript problems

Posted 07 March 2013 - 04:53 AM

I'd just like to point out that the methods used in those two scripts to execute the Javascript is pretty "old-school". In modern code you'd want to separate the Javascript and the HTML. That is: avoid putting Javascript directly into the HTML. Ideally all Javascript code should be in external script files, but excluding that for a moment, the code should at least be contained in <script> blocks inside the <head> or at the very bottom of the <body>. - Avoid using HTML attributes like "onclick" and "onload", in favour of Javascript events.

At it's simplest, the two scripts posted should be rewritten like so:
<!DOCTYPE html>
<html>
<head>
    <title>Using Redirect function</title>
    <script>
    window.onload = function() {
        setTimeout(function() {
            window.location = "http://www.google.com";
        }, 50000);
    }
    </script>
</head>

<body>
    <h2>Prepare to be redirected!</h2>
    <p>You will be redirected to the DBS site!</p>
</body>
</html>


<!DOCTYPE html>
<html>
<head>
    <title>Using button click event.</title>
    <script>
    window.onload = function() {
        document.getElementById("myButton").onclick = function() {
            alert("Welcome");
        }
    }
    </script>
</head>
<body>
    <h1>My first Javascript</h1>
    <p>
        Javascript can react to events. Like the click of a button:
    </p>
    <button id="myButton" type="button">Click Me!</button>
</body>
</html> 


Note that in both cases I am using the very simplest way to declare event handlers in Javascript. They can be - and sometimes must be - declared in a much more powerful way. That's a topic worth exploring!
Was This Post Helpful? 0
  • +
  • -

#6 9011  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-March 13

Re: javascript problems

Posted 10 March 2013 - 08:06 AM

i still don't really understand how to combine those two script all together in one page and the functions stay the same, would someone able to help me and explain clearly to me please
many thanks
Was This Post Helpful? 0
  • +
  • -

#7 Koyume  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 89
  • Joined: 19-February 13

Re: javascript problems

Posted 11 March 2013 - 12:52 PM

Basically what 9011 wants is both scripts to go into the same page (As in one HTML page) and execute the same way. The answer to the question is yes.

BUT!

Since one of your scripts is a redirection script, after 50 seconds (or 5000 milliseconds), the user that visits that page will be directed. Within that time, you want the user to click the button that you created, which will alert them with a box that says "Welcome!".

What andrewsw was trying to say is that your user who comes to this one page will have 50 seconds to click that button and get that alert. Can the combining work? Yes. But will your user stay if they are going to be redirected? Nope.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1