使用 javascript 将值传递到另一个页面
passing value to another page with javascript
我在 page0.html 中有两个标签。我想在 page1.html 中显示点击了哪个标签:
<a href="page1.html" target="_blank">A</a>
<a href="page1.html" target="_blank">B</a>
我想page1.html看起来像:
Link A was pressed
or
Link B was pressed.
可以使用 Javascript 吗?还是我应该使用其他东西?
非常感谢
您可以将哈希添加到 url(href) 并通过 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");
您也可以在 page0.html
中使用 ?
<a href="page1.html?a" target="_blank">A</a>
<a href="page1.html?b" target="_blank">B</a>
在page1.html
var parts = window.location.search.substr(1);
alert('Link '+parts+' was pressed');
以上答案可能是替代方案。我宁愿建议使用 document.referrer 来识别它。
更详细的可以参考Stack overflow查询:
How do you get the previous url in Javascript?
您可以通过 onclick 事件内联完成
<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>
我在 page0.html 中有两个标签。我想在 page1.html 中显示点击了哪个标签:
<a href="page1.html" target="_blank">A</a>
<a href="page1.html" target="_blank">B</a>
我想page1.html看起来像:
Link A was pressed
or
Link B was pressed.
可以使用 Javascript 吗?还是我应该使用其他东西?
非常感谢
您可以将哈希添加到 url(href) 并通过 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");
您也可以在 page0.html
中使用?
<a href="page1.html?a" target="_blank">A</a>
<a href="page1.html?b" target="_blank">B</a>
在page1.html
var parts = window.location.search.substr(1);
alert('Link '+parts+' was pressed');
以上答案可能是替代方案。我宁愿建议使用 document.referrer 来识别它。 更详细的可以参考Stack overflow查询: How do you get the previous url in Javascript?
您可以通过 onclick 事件内联完成
<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>