最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to refreshreload script tag by click? - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

You 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.

发布评论

评论列表(0)

  1. 暂无评论