Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    51 03' -78" N -114 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A Href Target with Javascript???

    Hello All,

    I am having trouble figuring out how to get the following link to load into a new window (target="_blank") see below

    <a href="#" onclick="updateLink(' + imgArray[x][3] + ');return false;" target="_blank">

    function updateLink(linkID){
    document.location.target='_blank';
    document.location.href="index.php?hitCount=1&linkID=" + linkID;
    }

  • #2
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    You can ditch the hardcoded target attribute, as you intend to add it via the js anyway. Also, once the js function has done its thing, you want the browser to act upon the new href, so you need to return true, not false.
    (Alternatively, not returning anything has the same effect.)

    i.e.
    Code:
    <a href="#" onclick="updateLink(this,imgArray[x][3]);">
    Code:
    function updateLink(obj,linkID){
    obj.target='_blank';
    obj.href='index.php?hitCount=1&linkID=' + linkID;
    }
    (In case you hadn't done so already, you should consider what non-js users get out of this link. If the content is essential to the site and not strictly js-related, then you should try to make that content accessible to those without js.)
    Last edited by Bill Posters; 10-29-2006 at 10:19 AM.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    51 03' -78" N -114 05' 72" W
    Posts
    617
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply,

    I'm curious would that be search engine friendly. (google)?

  • #4
    Senior Coder
    Join Date
    Feb 2003
    Posts
    1,665
    Thanks
    0
    Thanked 27 Times in 25 Posts
    Quote Originally Posted by Crash1hd View Post
    I'm curious would that be search engine friendly. (google)?
    Nope. Google would ignore it completely as it doesn't understand js.
    For most sites, Google is going to be the single most important non-js visitor they get.

    If you want the href to be set dynamically, but also followable by search engines, then you need to look at ditching js for a server-side option, such as PHP.

    A good rule of thumb is to build the site as if js didn't exist and then, once you've established that the site is accessible, usable and useful, lay on some js enhancements aimed at making the userflow even better for those with js-enabled UAs.


  •  

    Posting Permissions

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