How to execute javascript of one page on click to trigger a click event on another page?

自古美人都是妖i 提交于 2019-12-07 01:54:01

问题


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?


回答1:


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>



回答2:


On Page 2, paste this code

$(document).ready(function(){
if(window.location.hash == "#test4Handle1"){
document.getElementById("test4Handle1").click();
}
});



回答3:


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.



来源:https://stackoverflow.com/questions/7871927/how-to-execute-javascript-of-one-page-on-click-to-trigger-a-click-event-on-anoth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!