Mandeep Singh
Mandeep Singh

Reputation: 2146

javascript function from php like html href

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?

Upvotes: 2

Views: 1652

Answers (4)

Goutam Pal
Goutam Pal

Reputation: 1763

 echo '<a href="javascript:NewCal()">Link1</a>';

Upvotes: 2

usman mehmood
usman mehmood

Reputation: 112

yes you can cal function like:

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

Upvotes: -1

MrCode
MrCode

Reputation: 64526

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>';

Upvotes: 1

Harsha Venkataramu
Harsha Venkataramu

Reputation: 2904

Try this code

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

Upvotes: 0

Related Questions