Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Help with this JS code

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Question Help with this JS code

    Can somebody tell me why it wont work?
    <script language="javascript">
    document.getElementById('element').style.display = 'none';
    function click(e) {
    if (navigator.appName == 'Netscape'
    && e.which == 3) {
    document.getElementById('element').style.display = '';
    }
    else {
    if (navigator.appName == 'Microsoft Internet Explorer'
    && event.button==2)
    document.getElementById('element').style.display = '';
    }
    }
    document.onmousedown=click
    </script>
    <div id="element">hihihi</div>

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Wow, what an awesomely specific title.
    Don't set display to an empty value; instead, use 'block'.
    The language attribute is deprecated; the type attribute is required.
    Code:
    <script type="text/javascript">
    var z = document.getElementById('element');
    z.style.display = 'none';
    document.onmousedown = function(e) {
      if (navigator.appName == 'Netscape' && e.which == 3)
        z.style.display = 'block';
      else
        if (navigator.appName == 'Microsoft Internet Explorer' && e.button==2)
          z.style.display = 'block';
    }
    </script>
    <div id="element">hihihi</div>
    Last edited by Twey; 05-19-2006 at 06:56 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    what does z mean? how does it link to the div, "element"?

  4. #4
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    wait nevermind

  5. #5
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    I dont know why but it still doesnt work...

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    This works a lot better.
    Code:
    <script type="text/javascript">
    var z = document.getElementById('element');
    window.onload = function() {
      z.style.display = 'none';
      document.onmousedown = document.oncontextmenu = function(e) {
        if (navigator.appName == 'Netscape' && e.which == 3)
          z.style.display = 'block';
        else
          if (navigator.appName == 'Microsoft Internet Explorer' && e.button==2)
            z.style.display = 'block';
      };
    };
    </script>
    Last edited by Twey; 05-19-2006 at 07:47 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    thanx!
    Now im wondering how to make an element aligned with the cursor, i tryed this
    var w=1;
    while (w==1){
    menu.offsetLeft=Mouse.x;
    menu.offsetTop=Mouse.y;
    }
    it doesnt work though

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I take it you're trying to create something like this:
    Code:
    <script type="text/javascript">
    var z;
    function fakeMouseEvent(x,y) {
      this.pageX = x;
      this.pageY = y;
    }
    window.onload = function() {
      z = document.getElementById("element");
      document.onmousemove = function(e) {
        z.style.top = ((e.clientY ? e.clientY : e.pageY) + 30) + "px";
        z.style.left = ((e.clientX ? e.clientX : e.pageX) + 30) + "px";
      };
      document.onmousedown = document.oncontextmenu = function(e) {
        if (e.which && e.which == 3)
          z.style.display = 'block';
        else if (e.button && e.button == 2)
          z.style.display = 'block';
      };
    };
    </script>
    <div id="element" style="display:none;position:absolute;">hihihi</div>
    ?
    /EDIT: This one's IE-compatible.
    Last edited by Twey; 05-19-2006 at 08:09 PM.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default

    Is there a way to easily stop and start that code?

  10. #10
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    No, but you can just set display:none again on the element. There won't be a serious performance hit.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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