通过单击href javascript打开将打开的页面上的脚本

时间:2016-04-04 09:01:01

标签: javascript jquery html

我目前正致力于提供有趣的网络加载功能。我成功了 找到一个显示那些的脚本。但是我的问题是如何在点击href打开的页面上执行这个脚本

<html>
<head>
<script type="text/javascript">
// Add load event listener.
window.addEventListener("load", loadTime, false);

function loadTime() {
    var x = performance.timing.connectEnd+"f"; 

  // Get current time.
  var now = new Date().getTime();
  // Calculate page load time.
  var page_load_time = now - performance.timing.navigationStart;
  // Write the load time to the F12 console.
  if (window.console)  { console.log(window.performance.timing);
  window.alert(x)}

}
 </script>
 <script>
 var someAnchor = document.getElementById('someAnchor');

someAnchor.onclick = function(){
  loadTime();
};

function loadTime() {
  // Get current time.
  var now = new Date().getTime();

  // Calculate page load time.
  var page_load_time = now - performance.timing.navigationStart;

  // Write the load time to the F12 console.
  if (window.console) {
    console.log(window.performance.timing);
  }
}
 </script>
</head>
<body>
<a id="someAnchor" target="_blank" href="https://www.youtube.com/">youtube</a>

</body>
</html>

第一个脚本在当前页面上显示这些carachteristics,但第二个脚本在加载youtube页面时不起作用

2 个答案:

答案 0 :(得分:0)

所以你基本上只想在点击锚点时运行你的功能?

&#13;
&#13;
var someAnchor = document.getElementById('someAnchor');

someAnchor.onclick = function(){
  loadTime();
};

function loadTime() {
  // Get current time.
  var now = new Date().getTime();

  // Calculate page load time.
  var page_load_time = now - performance.timing.navigationStart;

  // Write the load time to the F12 console.
  if (window.console) {
    console.log(window.performance.timing);
  }
}
&#13;
<a id="someAnchor" href="#">some anchor</a>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你想要做的是非法和不可能的,由于安全原因,一旦你关闭页面或离开它,每个 javascript函数将立即停止。只是想象一下,如果每次离开页面时页面都会导航回来。