[英]Picking out the ID from a URL, add it to the end of a link with a new URL
我向公司客戶發送電子郵件,每個客戶都有唯一的ID。 當他們單擊郵件中的按鈕時,他們會轉到wordpress的登錄頁面,我將其ID保存在URL中。 例如:
http://www.example.com/?1234567
其中1234567是客戶ID號
我想要做的是有一個按鈕,該按鈕帶有一個從着陸頁到另一個域的鏈接,並且還發送帶有新網址tex的ID(當用戶單擊按鈕時,現在的地址為:)
http://www.newdomain.com/?1234567
如何使用javascript做到這一點?
您可以使用獲取當前網址
window.location.href
當您擁有當前URL時,可以使用?
進行拆分?
,以獲取ID。 將ID附加到新網址。 而已
您可以使用以下任一方式獲取查詢字符串
Java腳本
window.location.search
的PHP
$_SERVER['QUERY_STRING']
因此,要創建與此相關的鏈接,您可以執行以下任一操作:
Java腳本
<a href="#" onclick="window.location = 'http://www.newdomain.com/' + window.location.search;">LINK</a>
的PHP
<a href="http://www.newdomain.com/?<?php echo $_SERVER['QUERY_STRING']; ?>">LINK</a>
您可以使用window.location.search
來獲取ID。 請注意,在該示例中,由於iframe在網址中不包含ID,因此ID是undefined
的。
$(function() { var id = window.location.search.split('?')[1]; // get the id var link = $('#domainLink'); // get a refernce to the link var currentLink = link.prop('href'); // get the current url link.prop('href', currentLink + '?' + id); // change the url to include the id });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <a id="domainLink" href="http://whatever.com">link to another domain</a>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.