Sean De La Rosa Sean De La Rosa - 1 month ago 14
HTML Question

function rfrsh() not working when put on an external javascript file

my function "rfrsh()" is working when it is inside the html file but when i put it on an external javascript file, it is now working anymore. here is my code



/* JavaScript */

$(document).ready(function() {

function rfrsh() {
$("iframe").each(function() {
var src= $(this).attr('src');
$(this).attr('src',src);
});
}
});

<!-- HTML -->

<div id="circtraff" class="tab-pane active">
<ul class="nav nav-pills" id="circtraff_tabs">
<li><a href="#circtraff_realtime" data-toggle="tab" onClick="rfrsh()"><i class="glyphicon glyphicon-dashboard" aria-hidden="true"></i>&nbsp; Real-time Traffic</a></li>
<li><a href="#circtraff_last24" data-toggle="tab" onClick="rfrsh()"><i class="glyphicon glyphicon-time" aria-hidden="true"></i>&nbsp; Last 24 Hours</a></li>
</ul>
</div>





all other js functions are working except for the refresh function.

Answer

It's because you're scoping the rfrsh function to be local to the $(document).ready() function. To make it available to your HTML onClick event, define it in the window namespace, like so:

window.rfrsh = function() { /* ... */ };