简体   繁体   中英

Can I make an address link activate a javascript?

I currently have this code:

<input id="doCheck" type="button" onclick="doCheck('a'); return false;"  />

But I want to convert this to use an address link and not an input because I have created my own class btn and I want to use that.

<a class="btn" href="@(Model.NextUrl)" title="Go to next">&gt;&gt;</a>

Is it possible to do this. To make the clicking on the link activate Javascript. Also can I stop Google from getting interested in the address?

sure, you can add the onclick to the anchor:

<a class="btn" href="@(Model.NextUrl)" title="Go to next" onclick="doCheck('a'); return false;">&gt;&gt;</a>
<a href="ht..com" onclick="doCheck('a'); return false;" rel="nofollow">Link text</a>

the onclick can be used here rel="nofollow" will tell google jnot to 'click' the link

Yes, you can. The onclick attribute may be used with most elements .

<a class="btn" href="@(Model.NextUrl)" onclick="doCheck('a'); return false;"
   title="Go to next">&gt;&gt;</a>

By adding the onclick to your hyperlink, you avoid Google from tracking the address of the link since the JavaScript is not contained in the href . Either way, Google doesn't track JavaScript in href s anyway.

<a class="btn" href="@(Model.NextUrl)" title="Go to next"
    onclick="doCheck('a'); return false;">&gt;&gt;</a>

Alternatively, you can add a rel tag to any anchor that tells Google not to track the address of the hyperlink:

<a class="btn" href="@(Model.NextUrl)" title="Go to next"
    onclick="doCheck('a'); return false;" ref="nofollow">&gt;&gt;</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM