1

I have two tags in page0.html. I want to show in page1.html which a tag was clicked:

<a href="page1.html" target="_blank">A</a>
<a href="page1.html" target="_blank">B</a>

I want to page1.html look something like:

Link A was pressed

or

Link B was pressed.

is that possible using Javascript? or should i use something else?

Thanks a lot

Pravitha V
  • 3,238
  • 4
  • 29
  • 51
lucaxvu
  • 13
  • 3

4 Answers4

3

You can do add hash into url(href) and access by window.location.hash

page0.html

<a href="page1.html#A" target="_blank">A</a>
<a href="page1.html#B" target="_blank">B</a>

page1.html

console.log("Link "+window.location.hash.substring(1)+" was pressed");
David Jaw Hpan
  • 4,511
  • 3
  • 24
  • 50
0

You can also use ? in page0.html

<a href="page1.html?a" target="_blank">A</a>
<a href="page1.html?b" target="_blank">B</a>

In page1.html

 var parts = window.location.search.substr(1);
 alert('Link '+parts+' was pressed');
Dhruvang Gajjar
  • 536
  • 1
  • 9
  • 18
0

Above answers could be an alternative. I would rather suggest to use the document.referrer for identifying the same. For more details, please refer Stack overflow query: How do you get the previous url in Javascript?

0

You can do it inline by onclick event

  <a href="page1.html#A" onclick= "alert('Page1 was click') " target="_blank">A</a>
  <a href="page1.html#B" onclick= "alert('Page2 was click') " target="_blank">B</a>
bdalina
  • 459
  • 6
  • 15