Results 1 to 5 of 5

Thread: How to insert html link into this script

  1. #1
    Join Date
    Sep 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to insert html link into this script

    Im trying to put a a href link in the following code but the script dosent seem to work properly when href code is in.

    Code:
    var mycountdown = new Countdown();
    with (mycountdown) {
      tagID = "mycountdowndiv";
      setEventDate(2005, 10, 28, 5, 0, 0);
      event = "The Weekend!";
      onevent = "Wait for it...";
      afterevent = "Its the weekend!!!!";
    }
    addCountdown(mycountdown);
    Im trying to do this in the above
    Code:
    event = "The Weekend! <a href=http://www.something.com>link</a>";
    Can anyone please suggest another way of inserting the link without it disrupting/causing errors to the script?

    Thanks
    Net.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,997
    Thanks
    43
    Thanked 3,198 Times in 3,160 Posts
    Blog Entries
    12

    Default

    That should work but, this would be better:

    Code:
    event = 'The Weekend! <a href="http://www.something.com">link</a>';
    Hard to really say though, without seeing the full script and page that go with this snippet.

    PLEASE: Include the URL to your problematic webpage that you want help with.
    - John
    ________________________

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

  3. #3
    Join Date
    Sep 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Im still getting the same problem: the added link shows up as raw html.

    There are two files to this script, the first file is the only file i was told to customize:

    First .js file has this
    Code:
    var mycountdown = new Countdown();
    with (mycountdown) {
      tagID = "mycountdowndiv";
      setEventDate(2005, 10, 28, 5, 0, 0);
      event = "The Weekend!";
      onevent = "Wait for it...";
      afterevent = "Its the weekend!!!!";
    }
    addCountdown(mycountdown);

    Second .js file has this
    Code:
    var myCountdown = new Array();
    var repeat = false;
    
    function checkPlural(noun, value) {
      noun = ((value == 1) || (value == 0)) ? noun : (noun += "s");
      return noun;
    }
    
    function updateDisplay(text, id) {
      var tag = document.getElementById(id);
      if (tag.firstChild) {
        tag.firstChild.nodeValue = text;
      }
      else {
        textNode = document.createTextNode(text);
        tag.appendChild(textNode);
      }
      return;
    }
    
    function doCountdown() {
      for (i = 0; i < myCountdown.length; i++) {
        if (!myCountdown[i].expired) {
          var currentDate = new Date();
          var eventDate = myCountdown[i].eventDate;
          var timeLeft = new Date();
          timeLeft = eventDate - currentDate;
          msPerDay = 24 * 60 * 60 * 1000;
          msPerHour = 60 * 60 * 1000;
          msPerMin = 60 * 1000;
          msPerSec = 1000;
          daysLeft = Math.floor(timeLeft / msPerDay);
          hoursLeft = Math.floor((timeLeft % msPerDay) / msPerHour);
          minsLeft = Math.floor(((timeLeft % msPerDay) % msPerHour) / msPerMin);
          secsLeft = Math.floor((((timeLeft % msPerDay) % msPerHour) % msPerMin) / msPerSec);
          day = checkPlural("day", daysLeft);
          hour = checkPlural("hour", hoursLeft);
          minute = checkPlural("minute", minsLeft);
          second = checkPlural("second", secsLeft);
          if ((daysLeft == 0) && (hoursLeft == 0) && (minsLeft == 0) && (secsLeft == 0)) {
            updateDisplay(myCountdown[i].onevent, myCountdown[i].tagID);
          }
          else {
            if (daysLeft <= -1) {
              updateDisplay(myCountdown[i].afterevent, myCountdown[i].tagID);
              myCountdown[i].expired = true;
            }
            else {
              updateDisplay(daysLeft + " " + day + " " + hoursLeft + " " + hour +
                            " " + minsLeft + " " + minute + ", and " +
                            secsLeft + " " + second + " left 'til " +
                            myCountdown[i].event, myCountdown[i].tagID);
              repeat = true;
            }
          }
        }
      }
      if (repeat) {
        repeat = false;
        window.setTimeout("doCountdown()", 1000);
      }
      else {
        return;
      }
    }
    
    function setEventDate(year, month, day, hour, minute, second) {
      this.eventDate = new Date(year, month - 1, day, hour, minute, second);
      return;
    }
    
    function addCountdown(countdown) {
      myCountdown[myCountdown.length] = countdown;
      return;
    }
    
    function Countdown() {
      this.tagID = "";
      this.eventDate = new Date();
      this.setEventDate = setEventDate;
      this.event = "";
      this.onevent = "";
      this.afterevent = "";
      this.expired = false;
    }
    I then place this code where i want the script/countdown to appear
    Code:
    <div id="mycountdowndiv"></div>
    Which leads to an end product countdown script output of:

    1 day 16 hours 39 minutes, and 15 seconds left 'til The Weekend!

    This script works perfectly well except when i try to add
    <a href="http://www.something.com">link</a>
    into the first file the script keeps running but the added link comes up as raw html

    so the countdown script then looks like this:

    1 day 16 hours 39 minutes, and 15 seconds left 'til The Weekend! <a href="http://www.something.com">link</a>

    The problem is its treating the html as normal text. And thats, the low down

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,997
    Thanks
    43
    Thanked 3,198 Times in 3,160 Posts
    Blog Entries
    12

    Default

    Here is where our efforts are being frustrated:

    Code:
    function updateDisplay(text, id) {
      var tag = document.getElementById(id);
      if (tag.firstChild) {
        tag.firstChild.nodeValue = text;
      }
      else {
        textNode = document.createTextNode(text);
        tag.appendChild(textNode);
      }
      return;
    }
    In both cases, the text is applied as a text node. You want it to be used as html code. I still need to see the whole set up, page included, to test it and be sure but, I'd try replacing the above with this:

    Code:
    function updateDisplay(text, id) {
    document.getElementById(id).innerHTML=text;
    }
    - John
    ________________________

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

  5. #5
    Join Date
    Sep 2005
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks it worked! I can't believe that big piece of code I replaced could be substituted for a more condensed version, and still work? Stuff like that makes me wish my proffession was coding lol


    So Thanks again for the help!

    Neticus

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
  •