将地址栏变量中的当前URL传递到同一页面中的链接

时间:2022-12-08 11:20:03

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");

#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");