Ian Douglas
Ian Douglas

Reputation: 107

How to call js / script within href

I've been using this successfully in my template:

<span class="sharethis-text">
<p>More  Options</p>
</span>
<script src="http://w.sharethis.com/button/buttons.js"></script>
<script>
stLight.options({
publisher:'******0b-6eed-4740-81e7-aa3ee0bd9f85',
});
</script>

But now I want to call this function in this:

<More-sharebar><a href="#">More options</a></More-sharebar>

How is it possible to include the script properly?

Apologies if the answer is easy. I'm a complete beginner. I've been searching but I can't find how to do it.

Edit: Thanks for the answers so far, and I think now I have the function stLight.options( but I don't know how to include the external js file. Instead of editing the functions.php file, is it possible to simply include the script above in my HTML, but give it a name, and somehow call that name in the href? The other thing: the function as it original works triggers on hover. I'd like to retain that, if possible. Apologies for my ignorance.

Upvotes: 8

Views: 63332

Answers (6)

Moode Osman
Moode Osman

Reputation: 2013

Just put this Javascript inside your href:

<a href="javascript:;">More options</a>

Upvotes: 2

Nitin Jadhav
Nitin Jadhav

Reputation: 7336

Javascript supports following syntax :

<a href='javascript:alert('Hi')'>ClickMe</a>

This should solve your problem.

This is the reference Draft

Upvotes: 13

Arie Xiao
Arie Xiao

Reputation: 14082

  1. Write a function that calls the function you want. According to your post, maybe it's stLight.options? Return false if you don't want the navigating behavior of the a link.

    function clickHandler() {
        stLight.options({
            publisher:'******0b-6eed-4740-81e7-aa3ee0bd9f85',
        });
        return false;
    }
    
  2. Add the onclick handler to the a link.

    <More-sharebar><a href="#" onclick="javascript:clickHandler()">More options</a></More-sharebar>
    

Upvotes: 0

CGG
CGG

Reputation: 273

If you have in your file.js the:

function helloWorld(){...}

You call in the href with a event this function:

<a href="#" onClick="helloWorld()">More options</a>

Upvotes: 3

Mr.TK
Mr.TK

Reputation: 1836

In mark <a> define attrib:

    onclick="javascript:functionName();"

or

    onclick="return functionName(attribs)"

This should help.

But it should be done like this:

<a id="do_something">
   aaa
</a>

<script type="text/javascrip">
$.(function(){
   $('a#do_something').click(functionName());
});

Upvotes: 2

Gwennael Buchet
Gwennael Buchet

Reputation: 1307

in the HREF, you'll call the function you need, not the script file itself.

So you have to include your script in your HTML file (if possible at the end of the file, not in the header) and then call the method/function from your href.

Upvotes: 0

Related Questions