StudioTime
StudioTime

Reputation: 24019

Link not working onclick

I have a bookmarklet which a user can add to their browser, simple code:

javascript:(function(){ListIt=document.createElement('script');ListIt.src='http://***.com/js/list_it/feed.js';document.body.appendChild(ListIt);})();void(0);

I also want to have a button which a user can add to their own site and so want to do the same as above but onclick, but what I'm trying simply does nothing

Here's what I'm trying:

<a href="#" onclick="(javascript:(function(){ListIt=document.createElement('script');ListIt.src='http://***.com/js/list_it/feed.js';document.body.appendChild(ListIt);})();void(0);)"><img src="http://***.com/images_/icons/list-it-button.png" border="0" title="List It!"></a>

Upvotes: 0

Views: 852

Answers (2)

Rikki
Rikki

Reputation: 3538

Two thing I noticed about your code are:

First:

In this section: href="#" onclick="(javascript:( you missed that "javascript:" is a protocol detected by the browser to execute JavaScript code. It should be used in the URL values such as the value of the href attribute, not the onclick event. On the other hand, you can simply remove the javascript: from the onclick event and everything will work fine.

Second:

javascript: is like http: or https: and it can't start with parentheses like (javascript:(.

You should use this:

<a href="javascript:((function(){ListIt=document.createElement('script');ListIt.src='http://***.com/js/list_it/feed.js';document.body.appendChild(ListIt);})();void(0);)">
    <img src="http://***.com/images_/icons/list-it-button.png" border="0" title="List It!">
</a>

Upvotes: 1

epascarello
epascarello

Reputation: 207557

<a href="#" onclick="(function(){var ListIt=document.createElement('script');ListIt.src='http://example.com/js/list_it/feed.js';document.body.appendChild(ListIt);}())' return false;"><img src="http://example.com/images_/icons/list-it-button.png" border="0" title="List It!"></a>​

Upvotes: 0

Related Questions