Javascript events problem

Discussion in 'Web Design and Development' started by TheReef, Oct 5, 2007.

  1. TheReef macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #1
    Hi All
    There is probably a very simple solutoin to this, which I cannot see.
    All I want to do is print some text when a button is clicked:


    <script type="text/javascript">

    function write() {
    document.write("hello world")
    }

    </script>

    <input type="button" onclick="write()" value="Print hello world">



    What am I doing wrong? When I click on the button, all I get is a white screen.
    Thankyou.
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    There's a few ways to handle it. Here's a simple solution though.

    Add this to the body tag:
    Code:
    <div id="writehere"></div>
    Then you're JavaScript will be:
    Code:
    function write() {
      spot = document.getElementById("writehere");
      spot.innerHTML = "hello world";
    }
    The problem is that when you use document.write it rewrites the whole page. And like I said, there's multiple solutions to do what you need. Also you shouldn't name your function write since there is already a function named write, which you called. See how that might be a problem too?
     
  3. TheReef thread starter macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #3
    That worked great, thankyou!
    Another question,
    In the function that I have created, am I able to refference another external Javascript file?

    What I have done below doesn't seam to do anything when I click the button.
    (I changed the function name to "writing" btw).

    Code:
     <script type="text/javascript">
    function writing() {
    spot = document.getElementById("writehere")
    spot.innerHTML = spot.innerHTML + 
    "<script type=\'text/javascript\' src=\'external.js\'></script>"
    }
    </script>
    
     
  4. bootedbear macrumors 6502

    bootedbear

    Joined:
    Sep 13, 2004
    Location:
    Austin, TX
    #4
    When you import a JS file, it acts exactly as if you had included it inline.
     
  5. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #5
    Adding in another JS file isn't so easy, but check this article for details, http://www.webreference.com/programming/javascript/mk/

    But you can take another approach. Below is some code. When I wrote it up I had it all in the body tag, but doesn't need to be done that way. It basically outputs the output of the external javascript file into a hidden div tag, then when the button is pushed it changes the div style to block (which will allow it to be seen).
    Code:
    <div id="writehere" style="display:none;">
    <script type='text/javascript' src='test1.js'></script>
    </div>
    
    <script type="text/javascript">
    function writing()
    {
      spot = document.getElementById("writehere");
      spot.style.display = 'block';
    }
    </script>
    
    <input type="button" onclick="writing()" value="push me" />
    I'm not sure what you're trying to accomplish overall so an exact solution is hard to say, but this should let you know of an approach. If you need further details just ask.
     
  6. TheReef thread starter macrumors 68000

    TheReef

    Joined:
    Sep 30, 2007
    Location:
    NSW, Australia.
    #6
    Thanks heaps for this.
    I think I might be doing this the wrong way…

    Basically, my goal is to make a pushbutton be a download button for a file.
    When clicked, the external javascript file loads (which is really a php file) {I have that part sorted out}. The PHP script is then meant to load a counter file and add 1 to it so I know how many downloads of that file I get. {I also have that working}

    I don't know if the server will load the php file when opened by the javascript…

    There has got to be a better way of doing this :confused: :eek:
     
  7. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #7
    It sounds like AJAX will help you out. Just do some reading on it to see if it meets your needs. JavaScript can call PHP files using AJAX.
     

Share This Page