Results 1 to 4 of 4

Thread: Converting from embedded to external JavaScript problem with the "this keyword?"

  1. #1
    Join Date
    Nov 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Converting from embedded to external JavaScript problem with the "this keyword?"

    Hi
    some one gave me the script below which is almost exactly what I want if I could only converted to an external JavaScript file. The closest I have been able to come to doing this is to explicitly put the URL address fully written out between quotation marks where the variable name "URL" (in the below function) is passed the URL value just by using the"this.href" in the function call. I made several attempts to do this so I sure would appreciate some help. My reason for this want is this script is to be used twice on 27 pages.

    Very sincerely
    Marc

    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
     Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>delay</title>
    <meta http-equiv="Content-Type"
     content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="txt/javascript">
    <script type="text/javascript">
    function goURL(url){
    setTimeout(function(){window.location.href=url},100)
    }
    </script>
    </head>
    <body>
    <a href="http://www.google.com" 
    onclick="goURL(this.href);return false">
          mylink
    </a>
    </body>
    </html>

  2. #2
    Join Date
    Nov 2006
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    remove .href with goURL(this.href) so it'll read goURL(this)

    and in the function add url.href

    function goURL(url){
    setTimeout(function(){window.location.href=url.href},100)
    }


    that should work.

  3. #3
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by MarcMiller View Post
    some one gave me the script below which is almost exactly what I want if I could only converted to an external JavaScript file. The closest I have been able to come to doing this is to explicitly put the URL address fully written out between quotation marks where the variable name "URL" (in the below function) is passed the URL value just by using the"this.href" in the function call. I made several attempts to do this so I sure would appreciate some help.
    Your external file should contain:

    Code:
    function goURL(url) {
        setTimeout('location.href = "' + url + '";', 100);
    }
    You would then call that as you do now:

    HTML Code:
    <a href="http://www.google.com/" onclick="goURL(this.href); return false;">...</a>
    <head>
    <title>delay</title>
    <meta http-equiv="Content-Type"
    content="text/html; charset=iso-8859-1">
    If you're going to use a meta element to indicate the encoding scheme, rather than HTTP headers (the proper place for that information), include it as the first child of the head element. The browser should be notified as soon as possible before it might encounter non-ASCII characters (which may reasonably occur in a title element).

    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="txt/javascript">
    These two elements can be omitted. At the very least, fix the typo: text/javascript.


    Quote Originally Posted by Acey99 View Post
    remove .href with goURL(this.href) so it'll read goURL(this)
    Why? It doesn't significantly alter the behaviour of the code.

    that should work.
    The code already works. It's a bit daft, but it works.

    Mike

  4. #4
    Join Date
    Nov 2006
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Here's the conversion the way I actually achieved it. Thank you for those responses.

    HTML
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
    <title>relevant snippets from page</title>
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    <meta content="text/html; charset=us-ascii"
     http-equiv="Content-Type" />
     <script src="GaS.js" type="text/javascript"></script> 
    </head>
    <ul>
    <li id="prev">
     <a href="GaS2.html">
      <img  src="p1images/prevpointer.gif" alt="prev"/>
     </a>
    </li>
    <li id="return">
     <a href="../gallery.html">
      <img src="p1images/return.gif" alt="return" />
     </a>
    </li>   
    <li id="next">
     <a href="GaS4.html">
      <img  src="p1images/nextpointer.gif" alt="next" />
     </a>
    </li>  
    </ul>
    <embed src="click_x.wav" autostart=false width=0
     height=0 id="sound1" enablejavascript="true" volume="30">
    javaScript
    Code:
    function onClickDelayLocation(){
        goURL(this.href);
        return false;
            function goURL(url){
              onClickSound();          
              setTimeout(function(){window.location.href=url},100)
            };
    };
    function onClickSound () {
       var thissound=document.getElementById("sound1");
           thissound.Play();
    }
    window.onload = function () {
    document.getElementById("prev").getElementsByTagName('A')[0]
    .onclick=onClickDelayLocation;
    document.getElementById("return").getElementsByTagName('A')[0]
    .onclick=onClickDelayLocation;
    document.getElementById("next").getElementsByTagName('A')[0]
    .onclick=onClickDelayLocation;
    }

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
  •