1

I have a HTML page with two buttons. Both of these buttons referring same page, say Test2. While working on Test2(creating new elements etc..) I need to know which button was clicked. Here is an example code:

HTML1:

<head>
<title>Test 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<div class="Main" id="Main">
<div id="Div1"> 
<button id="Btn1"  onclick="window.location.href='Test2.html';"> this is Button 1 </button>
</div>
<div id="Div2"> 
<button id="Btn2"  onclick="window.location.href='Test2.html';"> this is Button 2 </button>
</div>
</div>

</body>
</html>

HTML2:

    <head>
    <title>Test 2</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
    <script type='text/javascript'>
if (1==1) { //if something happened
    var aaa=window.document.referrer.source;
    alert(aaa)}
    </script>
    </body>
    </html>

This code results with "undefined" alert message. How could I get id of button or div that referred to Test2.html?

osenel
  • 13
  • 4
  • 1
    There are multiple ways, one of them setting href to Test2.html?id=Div1 and then retrieve that on page2 – juvian Oct 20 '16 at 15:47
  • 1
    Make the link point to `Test2.html?id=Btn1` then you can read `Btn1` in Test2 with this: [How can I get query string values in JavaScript?](http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) – Alex K. Oct 20 '16 at 15:47

1 Answers1

1

Given the use of onclick to just set the new location I would add a query parameter to each URL:

<button … onclick=onclick="window.location.href='Test2.html?btn=1';" … >#

And then parse the query string in the code in Test2.html.

Richard
  • 106,783
  • 21
  • 203
  • 265