...

View Full Version : A Href Target with Javascript???



Crash1hd
10-29-2006, 09:51 AM
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;
}

Bill Posters
10-29-2006, 10:01 AM
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.

<a href="#" onclick="updateLink(this,imgArray[x][3]);">

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.)

Crash1hd
10-29-2006, 04:40 PM
Thanks for the reply,

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

Bill Posters
10-29-2006, 05:39 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum