SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd be interested to see what you tried.

    What's the point in adding a short delay in following the link?
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK here's a couple of things I tried.

    HTML
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <title>delay script</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 src="locationDelay.js" type="text/javascript">
    </script>
    </head>
    <body>
    <span id=dummyspan></span>
    <ul >
     <li id="xxx">
     <a href="http://www.google.com" >
        <img src="nextpointer.gif" alt=" " width="65" height="66">
     </a>
     </li>
    </ul>
    </body>
    </html>
    now on that HTML part of it I presently show my ID on the LI tag however I tried it with the ID on the anchor and on the image too an apparently the onclick event is on all three and putting the ID on the anchor tag doesn't help me on the using "this.href" in in my external file. A JavaScript script that works by explicitly scripting the URL is shown below followed by one that doesn't work utilizing the "this.href" like in the embedded example that does work.
    Code:
    function locationDelay() {
    setTimeout(function(){window.location.href="http://www.google.com"},7000);
    return false;
    };
    window.onload = function ()  {
    document.getElementById("xxx").parentNode.onclick=locationDelay;
    };
    Code:
    function locationDelay() {
    x=this.href;
    setTimeout(function(){window.location.href=x;},7000);
    return false;
    };
    window.onload = function ()  {
    document.getElementById("xxx").parentNode.onclick=locationDelay;
    };
    when I move my ID to the anchor tag I also remove the "parentnode" between "document.getElementById("xxx")" and the rest of that line. But that doesn't work either. The reason I have my ID on the LI tag is I am modeling the way I haven't on these 27 pages this script is intended for. The reason for the short delay is to give a very short and low volume clicking sound time to sound. Most browsers have a conflict if you start a sound in one location and immediately go to another. I know I've been told this sound may not be a good idea but I intend to ask on a design forum once my implication of this idea is working even know it's against some kind of golden rule. So that's it any help you could give me would be greatly appreciated.
    Sincerely
    Marc

  4. #4
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    //in external script
    function goURL() {
      window.location = this.href;
    }
    Code:
    <a id='mylink' href='..'>..</a>
    <script type='text/javascript'>
    document.getElementById('mylink').onclick = goURL;
    </script>
    Just attach the function to whatever event you want on whatever elements, and the 'this' var is available within the function

  5. #5
    SitePoint Zealot
    Join Date
    Oct 2006
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for those responses they were quite helpful.
    Marc


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
  •