Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

anchor

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?

like image 783
David Diller Avatar asked Oct 24 '11 06:10

David Diller


2 Answers

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>
like image 141
SatishJS Avatar answered Oct 21 '22 10:10

SatishJS


On Page 2, paste this code

$(document).ready(function(){
if(window.location.hash == "#test4Handle1"){
document.getElementById("test4Handle1").click();
}
});
like image 23
Bharath Avatar answered Oct 21 '22 09:10

Bharath