www.webdeveloper.com
Results 1 to 6 of 6

Thread: simple practice script - alert not working/in wrong place?

  1. #1
    Join Date
    Mar 2009
    Location
    Sydney Australia
    Posts
    3

    simple practice script - alert not working/in wrong place?

    Hello,
    This is my first post - I hope this is an appropriate question...please tell me if this is not appropriate.

    here is my practice script - here I'm trying to get an alert box to appear when clicking on a link containing the Id "redirect". When I run this and click the link the alert box doesn't come up.

    I've tried putting the alert command in various other places and no luck.


    (the javascript)

    window onload = initAll;

    function initAll(){
    document.GetElementById("redirect") .onclick = initRedirect;
    }


    function initRedirect(){
    alert("you are being redirected");
    window.location = "2nd html page.html";
    return false;
    }



    (the html link)

    <h2>
    <a href = "2nd html page.html" id = "redirect">Welcome to our site, come on in</a>
    </h2>

  2. #2
    Join Date
    Nov 2003
    Location
    Worthington, OH, USA
    Posts
    3,634
    Little "g" in getElementById, and an anonymous function for the onclick.
    Code:
    window.onload = initAll;
    
    function initAll(){
    document.getElementById("redirect").onclick = function () { initRedirect() };
    }
    
    
    function initRedirect(){
    alert("you are being redirected");
    window.location = "2nd html page.html";
    return false;
    }
    Last edited by TheBearMay; 03-31-2009 at 08:54 PM.
    54 68 65 42 65 61 72 4D 61 79

  3. #3
    Join Date
    Mar 2009
    Location
    Sydney Australia
    Posts
    3
    Thanks for replying, TheBearMay

    also - after copying and pasting your reply, it worked (of course) - and then I realised I also had left the . out of window.onload

    - just to experiment I then ran it with this -

    function initAll(){
    document.getElementById("redirect").onclick = initRedirect;
    }


    - with what you said was the anonymous function and it still worked - probably not best practice I'm guessing...

    thanks again - after a couple of wasted hours I can continue!

  4. #4
    Join Date
    May 2006
    Location
    Odenton, MD
    Posts
    1,449
    Quote Originally Posted by jsneophyte View Post
    ...anonymous function and it still worked - probably not best practice I'm guessing...
    is still valid. both ways, anonymous function or named function are valid. consider for example if you have to pass a parameter to the function and you want that function as a handler of an event, how you would do it? in that case, anonymous function come handy. again, both way are valid, is a matter of taste and in some case of care.
    my mom is javascript, dad is javascripter, granpa is javascriptor, and my little sister is javasRidiculous.
    my nature language is javascript, then come spanish and english -- me

  5. #5
    Join Date
    Mar 2009
    Location
    Sydney Australia
    Posts
    3
    Quote Originally Posted by ZeroKilled View Post
    is still valid. both ways, anonymous function or named function are valid. consider for example if you have to pass a parameter to the function and you want that function as a handler of an event, how you would do it? in that case, anonymous function come handy. again, both way are valid, is a matter of taste and in some case of care.
    Thanks ZeroKilled - I'm just studying passing values to functions now, have been for an hour or so - what you said is really above me at this stage - I can (sort of) see how passing values works when I look at an example code, but there's no way I could write another example out myself -

    (I'm finding JS is just freaking hard at the moment...)

  6. #6
    Join Date
    Nov 2003
    Location
    Worthington, OH, USA
    Posts
    3,634
    The only reason I used the anonymous function in this case was that some browsers have issues if you try assigning functions using javascript to events tied to HTML elements post-rendering of the page if you don't use the anonymous function.
    54 68 65 42 65 61 72 4D 61 79

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles