簡體   English   中英

在Javascript中將值附加到Form上的URL

[英]Append the values to the URL on Form in Javascript

我在多個頁面上有一個表單鏈接。 當我點擊頁面A的表單鏈接時,我需要的是它應該在URL上顯示為www.form.com?referrer=Page A ,當我提交表單時,我創建了一個隱藏的字段referrer ,當我收到它時在推薦人字段上的電子郵件,它應顯示“頁面A”。

我有這個HTML代碼工作正常但是,我不想在每個頁面上手動執行,只要用戶點擊表單鏈接或收到它應該自動更新的表單:

<a target="_blank" title="Click Here" href="https://myform.com/forms/land_discount?referrer= Beach Club">

這是我的表單的JavaScript:

折扣表格

看看這是你想要做的:

// I'm going to use jQuery, it's easier for me
$(document).ready(function(){
    // I used a class so it doesn't do it to every <a> tag
    $('a.use_refer').click(function(e){
        // Stop the button from submitting normally
        e.preventDefault();
        // Create a new url
        // You may or may not want to use encodeURIComponent() on the path value
        var newUrl  =   $(this).attr('href')+'?referrer='+document.location.pathname;
        // Go to the new page
        window.location = newUrl;
    });
});
</script>

<a href="https://myform.com/forms/land_discount" title="Online Form" class="use_refer">Click Here</a>

假設您的Page A是路徑的最后一個目錄。 並在DOM准備就緒時將其包裝起來。

請注意添加id=標記對HTML的更改。

在“頁面A”

HTML:

 <script type="text/javascript" src="https://myform.com/forms/js.php/test"></script>
 <noscript>
   <a href="https://myform.com/forms/test" title="Online Form">Game Form</a>
 </noscript>

使用Javascript:

var url = document.getElementById('formRef').getAttribute("href");
document.getElementById('formRef')
.setAttribute('href', encodeURI(url + '?referrer=' + window.location.href.slice(window.location.href.lastIndexOf('/') + 1)));

論“登陸頁面”

HTML:

<a id="landingPage" target="_blank" title="Click Here" href="https://myform.com/forms/land_discount">

使用Javascript:

var href = document.getElementById('landingPage').getAttribute('href');

if(window.location.href.indexOf('referrer=') > -1){
    document.getElementById('landingPage').setAttribute('href', encodeURI(href + window.location.href.slice(window.location.href.indexOf('?referrer='))));
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM