將地址欄變量中的當前URL傳遞到同一頁面中的鏈接

[英]Passing Current URL in address bar variable to a link in the same page


What I am trying to achieve is :

我想要實現的是:

  1. Get Current URL Address from browser bar which will look like this http://example.com/test/index.html?&dv1=1023faf2ee37cbbfa441eca0e1a36c
  2. 從瀏覽器欄獲取當前URL地址,如下所示http://example.com/test/index.html?&dv1=1023faf2ee37cbbfa441eca0e1a36c

  3. Get the long ID number at the end of the URL 1023faf2ee37cbbfa441eca0e1a364
  4. 獲取URL末尾的長ID號1023faf2ee37cbbfa441eca0e1a364

  5. Dynamically pass it into another link in the same page http://example.org/?1023faf2ee37cbbfa441eca0e1a364 passing it as a variable to this code onclick="nextQuestion(6, 'http://www.example/iclk/redirect.php?apxcode=042004&id=eT9HmN9XD3xMgT8nKUj0KRjM‌​IWuXeTj0KN2-0N&dv1={id}');" repacing the last part id.
  6. 動態地將它傳遞到同一頁面中的另一個鏈接http://example.org/?1023faf2ee37cbbfa441eca0e1a364將其作為變量傳遞給此代碼onclick =“nextQuestion(6,'http://www.example/iclk/redirect.php? apxcode = 042004&ID = eT9HmN9XD3xMgT8nKUj0KRjM IWuXeTj0KN2-0N&DV1 = {ID}');”重新調整最后一部分ID。

I need to do this using jquery or javascript NO PHP

我需要使用jquery或javascript NO PHP來做到這一點

2 个解决方案

#1


2  

I completely reworked this answer. I think I now understood your problem.

我完全重寫了這個答案。我想我現在明白你的問題。

Generally spoken it's a bad idea to inline event handlers. You should separate structure and behaviour. You can easily do this by binding the handlers inside your javascript, instead of in your markup.

一般來說,內聯事件處理程序是個壞主意。你應該分開結構和行為。您可以通過綁定javascript中的處理程序而不是標記來輕松完成此操作。

To do this you just have to

要做到這一點,你只需要

  1. Remove the onclick from the button, add an identifier (question)

    從按鈕中刪除onclick,添加一個標識符(問題)

    <a class="btn rollover question">click me</a>
    
  2. Then you can do

    那你可以做

    $('.question').click(function() {
        var id = getQueryVariable("dv1");
        nextQuestion(6, 'http://www.example/iclk/redirect.php?apxcode=042004&id=eT9HmN9XD3xMgT8nKUj0KRjM‌​IWuXeTj0KN2-0N&dv1=' + id);
    });
    

You might want to check the full example in the fiddle. It's not working because the fiddle runs inside an iframe, but as soon as you copy it to your project, it'll work.

您可能想要查看小提琴中的完整示例。它不起作用,因為小提琴在iframe中運行,但只要你將它復制到你的項目中,它就會起作用。

Oh and one last thing: You might want to use a library to get parameters from the url. The safest, easiest & most reliable way of doing stuff is to use libraries. In your case you might want to try simple-query-string.

哦,最后一件事:你可能想用一個庫從url獲取參數。最安全,最簡單,最可靠的做法是使用庫。在您的情況下,您可能想嘗試simple-query-string。

#2


0  

I used the following function to get the part I need from the link

我使用以下函數從鏈接獲取我需要的部分

function getQueryVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}


var foo = getQueryVariable("dv1");

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2016/05/27/724e37ffec26f91f1707bddce75fba46.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com