/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Want to mimic <A> tag in Javascript

I want to mimic the action of an <a> tag click in Javascript. While this may sound simple, I need my javascript function to take two arguments: href and target. I also need it to handle targets like ‘_blank’ or _‘self’ or whatever else you can supply as a target.

I’m hoping someone has already written this and I can just steal it. Any friendly folks out there who can help me out?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@drlarkMar 07.2008 — There's a copy of ways to handle it, but generally you'd want to do add a onclick event to the encapsulating object. You could do something like this:

<i>
</i>&lt;script language="javascript"&gt;
function openwin(url,tgt){
var newWin=window.open(url,tgt);
newWin.focus();
}
&lt;/script&gt;
.
.
.
&lt;div onclick="openwin('other.html','another_win');"&gt;Some other location&lt;/div&gt;

Obviously you'd have to check for psuedo windows like _top, _self, etc.

Hope this helps.

-dan
Copy linkTweet thisAlerts:
@gil_davisMar 07.2008 — See if this covers all your bases:
<i>
</i>function fakeALink(hrf, tgt) {
if ((tgt == "") || (tgt == "_self"))
{window.location.href = hrf;
return;}
else
{window.open(hrf, tgt);
return;}
}
Copy linkTweet thisAlerts:
@TaboMar 07.2008 — I made the code for you, however I didn't save it and front page is giving me that "server is busy" message, if I get the script back I'll do post it. Also it is better for you to remove the computed style of the a tag to get the a tag to look like any other tag...
Copy linkTweet thisAlerts:
@sneakyimpauthorMar 07.2008 — BANG!

Thanks guys. Gil's function works perfectly.
Copy linkTweet thisAlerts:
@TaboMar 07.2008 — Mine works BETTER!&lt;html&gt;

&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1252"&gt;
&lt;title&gt;Click me&lt;/title&gt;
&lt;style type="text/css"&gt;
.link{
color:blue;
text-decoration:none;
cursor:pointer;
cursor:hand;
}
body link:hover{
text-decoration:underline;
color:red;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
window.onload=renderlinks;
if(window.onpageshow){
window.onpageshow=renderlinks; // Firefox - when user presses &lt;- and the page is not reloaded, this refreshes the state.
}

function renderlinks(){
elements=document.getElementsByTagName("span"); //this may be slow on crap computers and use CPU, however shouldn't be a big problem.
for(i=0;i&lt;elements.length;i++){
if(elements[i].className==="link"){
target=elements[i].getAttribute("target")?elements[i].getAttribute("target"):"_self";
href=elements[i].getAttribute("href")?elements[i].getAttribute("href"):"#";
if(document.attachEvent){
elements[i].attachEvent("onclick",function(){
if(target==="_blank"){
win=window.open(href,'dsfsd');
if(!win){
alert("You have a popup blocker -.-");
}
}
else{
window.location=href;
}
})
}
else{
elements[i].addEventListener("click",function(){
if(target==="_blank"){
win=window.open(href,'fsdf');
if(!win){
alert("You have a popup blocker -.-");
}
}
else{
window.location=href;
}
},false)
}
}
}
}

//NOTE: Links cant have more than one class names, it is possible and easy to edit the script so they can.
&lt;/script&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;span class="link" target="_blank" href="http://www.something.com"&gt;Click me&lt;/span&gt;
&lt;/body&gt;

&lt;/html&gt;
Copy linkTweet thisAlerts:
@ShampieMar 08.2008 — I dont think this is a race in who can write a better code but non-theless all solutions are ways to learn from.

I did not know you could put a href in a span tag :/

cool!

Tnx!
Copy linkTweet thisAlerts:
@sneakyimpauthorMar 08.2008 — You can type any attribute you like in any tag you want:
[code=php]<span myAttr="someValue"id="mySpan">this is my span</span>[/code]

and access it from javascript like this:
<i>
</i>var el.documentGetElementById('mySpan);
var myval = el.gettAttribute('myAttr');


[/code]
×

Success!

Help @sneakyimp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...