![](/img/trans.png)
[英]How do I make a div link open in a new tab Javascript onClick="window.location
[英]javascript - How do I make onclick“window.location” also work when user opens in new window
為了隱藏鏈接的真實目的地,我使用以下代碼:
<a href="http://example.com" onclick="window.location='http://url-i-want-to-hit.com/'; return false;">Click me</a>
但是,如果我單擊鼠標右鍵,然后在新窗口中打開,它將轉到anchor href標記中指定的url。 我希望它轉到javascript中指定的網址。
有沒有辦法做到這一點?
提前致謝
您只需要一個mousedown處理程序即可偽造它們。
<a href="http://example.com"
onmousedown="this.href2 = this.href;
this.href = 'http://url-i-want-to-hide.com/';"
onmouseout="if(this.href2) this.href = this.href2;">Test</a>
這是它工作的現場演示 。
但是,如果我單擊鼠標右鍵,然后在新窗口中打開,它將轉到anchor href標記中指定的url。 我希望它轉到javascript中指定的網址。
否,但是您可以使用JS在文檔加載后(或在鏈接的onfocus
和onhover
事件中,將鏈接href設置為隱藏的URL,盡管這聽起來onhover
和不完整)。
<a onfocus="this.href='hidden_url'" .....>
這是Google用來計算搜索結果頁面中傳出鏈接的方式。
如何將真實網址存儲在數據庫中並在php中調用它們?
www.yourwebsite.com/url.php?id=3
url.php //類似
$query = "SELECT * FROM `urls` WHERE `id` = " . intval($_GET['id']);
$row = mysqli_fetch_assoc($query);
header ("location: $row['url']");
你做不到 右鍵單擊使用真正的錨點,而您將使用javascript覆蓋錨點的單擊功能。 有點騙人嗎?
編輯:也許我不清楚...抱歉。 使用“#”將阻止他們在新窗口中打開其他頁面。 這可能不適用於您的情況,並且我不知道您是否通過隱藏URL嘗試通過SEO實現某些目標。 除了href中的“#”外,我的解決方案幾乎與您當前的操作相同,我只希望將window.location包裹在一個函數中。...個人喜好。
<a href="#" onclick="SendToUrl(); return false;"">Click Here</a>
<script language="javascript">
function SendToUrl(){
window.location='http://url-i-want-to-hide.com/'
}
</script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.