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,308
  • 4
  • 33
  • 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");
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52
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
  • 568
  • 1
  • 8
  • 20
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
  • 503
  • 10
  • 16