Results 1 to 9 of 9

Thread: use js call itself as a string within a function

  1. #1
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default use js call itself as a string within a function

    I need the js call itself as a string within a function ..

    Something like this:
    Code:
    <div id="myElement" >
    <a href="javascript:myFunction('foo','bar','myElement');otherFunction('fiddle');randomFunction()">this&raquo;
    </a></div>
    
    myFunction (arg1,arg2,element) {
       document.getElementById(element).display="none";
       // I wish to create a new
       // button/link element which calls
       // exactly the same js as the link above did, i.e.:
       // "javascript:myFunction('foo','bar','myElement');otherFunction('fiddle');randomFunction()"
    }
    Of course, in different elements, there might any random js calling myFunction().

    Any suggestions?

  2. #2
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quite often when you ask a question, you come up with the answer yourself ..:

    Code:
    <a id="tjek" href="javascript:alert(thisLink('tjek'));moveElement('wrap','200px','100px','0px','0px')">move&raquo;
    </a>
    
    function thisLink(e) {
       var myE=document.getElementById(e);
       return myE.href;
    }
    I would of course be nice if I could reference to the current node without inventing an id for it ..
    Last edited by jonas-e; 03-08-2009 at 11:01 AM.

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

    Default

    Link it.
    Code:
    <a href="javascript: alert(this.href);
                         moveElement('wrap', '200px', '100px', '0', '0');">
      move»
    </a>
    However, this is a fairly peculiar thing to be doing. I suspect there's a better way, but I can't really tell you without more knowledge of what you're attempting.
    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!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    First of all:

    Code:
    href="javascript:
    is not such a hot idea. In IE 6, probably other IE versions like 7 and even 8, using that construct tells the browser that when the link is clicked, it is supposed to begin the onunload process of the page. This will stop the animation of animated .gif images, halt processing in some javascript and Flash. Not anything you generally want to have happen. This isn't what usually happens in most other browsers, but it isn't contrary to the standards, so others may adopt it or already exhibit it.

    With that in mind, have a look at this demo:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">
    function func(id){
     id = document.getElementById(id);
     id.onclick.apply(id);
    }
    </script>
    </head>
    <body>
    <div>
    <a href="#" id="testlink" onclick="alert(this.firstChild.nodeValue);return false;">Test Link</a><br>
    <input type="button" onclick="func('testlink');" value="Go!">
    </div></body>
    </html>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey:
    Thanks, I tried this.href - it returns "undefined".

    I figured out a different way of doing things - but basically, I wanted to create a href/button that would toggle the element it was nested inside - hence the link would no longer exist. So I wanted to take all the javascript from that href and move it to another one - and thus be able to toggle it forth and back between two elements.

    jscheuer1:
    Thanks for the tip about the onclick attribute - I didn't know about that one. Makes life a lot easier as I can put that on to e.g. a div tag - thus I don't have to wrap an <a href> around it ..!

    Never mind about the IE support - it was just a mock-up to show my colleagues an idea of mine about a new UI:
    http://lookfeel.ellehauge.net/
    - works fine in proper browsers like Firefox, Chrome and Safari

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

    Default

    Twey:
    Thanks, I tried this.href - it returns "undefined".
    Not as I used it, it won't. Inside another function, should you call it normally from the handler, this will be window, and therefore this.href will be undefined. The usual approach would be to pass the element in as an argument:
    Code:
    function foo(el) {
      alert(el.href);
    }
    
    ...
    
    <a href="#" onclick="foo(this);">Foo</a>
    ... but you could also pass it as the context of the new function call:
    Code:
    function foo() {
      alert(this.href);
    }
    
    ...
    
    <a href="#" onclick="foo.call(this);">Foo</a>
    Edit: Oh, yes, I forgot you were using javascript: URIs instead of proper event handlers. One more reason not to.
    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
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, I will try that out at some point

    Forgive me for asking weird questions - as I am not a developer, I do [EDIT TYPO: NOT] know how to structure code. I just try the best I can ..

    I dont' get your comment:
    Edit: Oh, yes, I forgot you were using javascript: URIs instead of proper event handlers. One more reason not to.
    Last edited by jonas-e; 03-12-2009 at 02:17 PM.

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

    Default

    A javascript: URI is executed in the global scope, but an event handler is executed in its own scope with the element as context and a special event object available to give you more information about the event.
    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
    Feb 2007
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks a bundle, I think I understand now.

    Please note significant corrected typo in my post above.

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
  •