Here is my code. I want to refresh/reload script tag and div content by a click.
<div id="mydiv"></div>
<a id="refresh">click</a>
<script id="mydiv">
$("#mydiv").css( //some_css )
</script>
<script>
$(function() {
$("#refresh").click(function() {
$("#mydiv").load("location.href + " .mydiv")
})
})
</script>
Here is my code. I want to refresh/reload script tag and div content by a click.
<div id="mydiv"></div>
<a id="refresh">click</a>
<script id="mydiv">
$("#mydiv").css( //some_css )
</script>
<script>
$(function() {
$("#refresh").click(function() {
$("#mydiv").load("location.href + " .mydiv")
})
})
</script>
Share
Improve this question
edited May 28, 2015 at 11:35
Ritesh Sinha
8406 gold badges23 silver badges50 bronze badges
asked May 28, 2015 at 11:31
BunuiBunui
331 gold badge2 silver badges10 bronze badges
3
- create a function and refresh that function on click. – Hushme Commented May 28, 2015 at 11:32
- and load that function at start ;) – aorfevre Commented May 28, 2015 at 11:32
- Id be unique in html div and script have same id – Sudharsan S Commented May 28, 2015 at 11:33
1 Answer
Reset to default 5You can force the script to reload by adding a random variable after its URL.
$("button").click( function(){
var src = $(".reloadMe").attr('src');
// Reload the script
$(".reloadMe").attr('src', src.split('?')[0] + "?" + Math.floor(Math.random()* 10000000));
// Reload the div
$("#mydiv").load(location.href + " .mydiv")
});
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script class="reloadMe" src="js/myScript.js"></script>
<button>Reload script</button>
This will produce :
src="js/myScript.js?61416981"
then
src="js/myScript.js?66541657"
then
src="js/myScript.js?14882875"
etc., forcing the browser to reload the script each time the button is clicked.