How to execute javascript of one page on click to

2019-06-23 07:57发布

This is what the link looks like on page 1:

<li id="click1">
  <a href="products.htm#test4Handle1">TNT Cable System</a>
</li>`

This is what I want to trigger on page 2:

<a name="test4Handle1">
  <button onclick="$('#test4Handle1').click()">TNT Cable System</button>
</a>

my attempt at jquery

$("test4Handle1").observe('domready',function() {
  document.getElementById("test4Handle1").click();
});

What page should have the javascript page 1 or 2?

标签: anchor jquery
3条回答
Anthone
2楼-- · 2019-06-23 08:26

If I understand you correctly, I would have to say that what you are looking for is not possible. Scripts are jailed to the pages on which they are loaded in. While it is possible for them to trigger the creation of new windows, it is not possible to interact with those windows, their DOM's and the scripts they are running.

查看更多
小情绪 Triste *
3楼-- · 2019-06-23 08:35

On Page 2, paste this code

$(document).ready(function(){
if(window.location.hash == "#test4Handle1"){
document.getElementById("test4Handle1").click();
}
});
查看更多
冷血范
4楼-- · 2019-06-23 08:39

Check if this helps you. Following is one example I got working

Here is Page1.html

<!DOCTYPE html>
<html>
<head>
  <title>Page1</title>
</head>
<body>
  <h2>Page1</h2>
  <a href="page2.html?event=true">Click here to go on page2</a>
</body>
</html>

And here Page2.html

<!DOCTYPE HTML>
<html>
<head>
  <title>Page2</title>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js" >   
  </script>
  <script type="text/javascript">
    $(function() { 
      var prevURL = (window.location.search).contains("event=true");
      if(prevURL == true)
        $("#btn_click").click();
      else
        alert("Not proper prev page");
     });
  </script>
</head>
<body>
  <h2>Page2</h2>
  <a href="page1.html">Click here to go on page1</a>

  <input type="button" id="btn_click" value="Test Button Click"/>
  <script>
    $("#btn_click").click(
       function() {
         alert("Button CLicked From jQuery");
       });
  </script>
</body>
</html>
查看更多
登录 后发表回答