2

I am calling the javascript function from another file in href, I want to know if there is any way to call it in php without using href?

call from href

<script language="javascript" type="text/javascript" src="js/myjs.js"></script>
<a href="javascript:NewCal()">Link1</a>

how can I use "javascript:NewCal()" in PHP echo?

1
  • I'm not entirely sure but I think you should simply be able to echo "<script>NewCal();</script>";. If you want to do this after page load, however, you'll want to look into Ajax. Commented Jun 13, 2013 at 9:46

4 Answers 4

2
 echo '<a href="javascript:NewCal()">Link1</a>';
Sign up to request clarification or add additional context in comments.

Comments

1

PHP can't directly call a Javascript function because PHP runs on the server side and Javascript on the client.

What you can do is echo the Javascript call into an onclick handler or any other event handler for example:

<?php
    echo '<a href="javascript:NewCal()">Link1</a>';
    echo '<p onclick="NewCal()">Click me</p>';
?>

If you want to execute it on load:

echo '<script>window.onload = function(){ NewCal(); };</script>';

2 Comments

I need to call this function without user, if I use <a href> user need to click on that
See my example of calling it on load.
0

Try this code

echo "<a href=\"javascript:NewCal();\">";

1 Comment

or just echo '<a href="javascript:NewCal();">';
-1

yes you can cal function like:

    <a href = "#"> <spam name="spmNewCal" onclick="javascript:NewCal()" id="spmNewCal" /></a>

Comments

Your Answer

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.