0

I am having a "duh" moment in which I am trying to execute a JavaScript function via an a href.

<a href="javascript:test()">Open New Form</a>

The function is stored in an external JavaScript file. If I have it inline with my function within <script> tags and the a href after it, it works.

What is the best method to execute a JavaScript function stored in another file?

p.campbell
101k71 gold badges265 silver badges326 bronze badges
asked Aug 7, 2014 at 21:15
5
  • 1
    Did you add the file using <script src="/path/to/file.js"></script> ? If so, you can call functions from there like they were in the same file. Commented Aug 7, 2014 at 21:21
  • Yes, which is odd. I may try to use jQuery's click Commented Aug 7, 2014 at 21:26
  • Probably you can use ExecuteOrDelayUntilScriptLoaded(test,'scriptname'); Commented Aug 7, 2014 at 21:27
  • Agreed. Nothing showing up except that it is undefined. Any good articles on how to do this in jQuery? Commented Aug 7, 2014 at 21:47
  • Wait. I am loading the JavaScript files via the XML Viewer Web Part so while they show as included, it is post DOM load. Should that matter? Commented Aug 7, 2014 at 22:50

4 Answers 4

1

If you're not linking to another page, try using the <button> element:

<button onclick="test()">Open a New Form</button>

Also, make sure your JavaScript file is properly loading on the page.

answered Aug 7, 2014 at 21:25
Sign up to request clarification or add additional context in comments.

Comments

1

If you use

<a href="javascript:test()">Open New Form</a>

and keep "test" function outside the page then also it will work as long as you are giving the reference of that javascript in your page like <script src="path"></script>

As you wanted the onclick function of the element so its good/best to use

<a onclick="test()"></a>
answered Aug 7, 2014 at 21:36

Comments

0

The issue was tied to caching as I had made updates to my JavaScript file but it wasn't showing. I resolved this, and then my core issue by using the following jQuery in my JavaScript file to call the function on click:

 $('#commentlink').click(function () {
 addComment();
});

This is called in the HTML like this:

<a class="pointer" id="commentlink" onclick="javascript:OpenPopUpPage();">Add Comment</a>
answered Aug 8, 2014 at 4:49

Comments

0

You can use:

<a href="javascript:" onclick="test()">Open New Form</a>
answered Aug 8, 2014 at 10:39

1 Comment

I think it would be more helpful for the OP and further visitors, when you add some explaination to your intension.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.