How to set specific query string value (or update if exists) without appending the value on click of a <a> tag?

huangapple go评论66阅读模式
英文:

How to set specific query string value (or update if exists) without appending the value on click of a <a> tag?

问题

我正在尝试在单击<a>元素时更新或设置特定的URL查询字符串值。

我设法通过设置URL来使其工作:

<a id="my-link" href="http://example.com/"></a>

$('#my-link').on('click', function(e) {
    let url = this.getAttribute('href');
    let queryStringKey = 'someKey';
    let queryStringValue = encodeURIComponent(someDataObject);
    url += `?${queryStringKey}=${queryStringValue}`;
    e.originalEvent.currentTarget.href = url;
});

但这仅在第一次单击时有效,因为它不断附加查询字符串。

我尝试过保存原始URL,使用e.originalEvent.currentTarget.href设置URL,然后在单击后设置回原始URL,但它不起作用,它只读取原始URL而不是更新后的URL:

<a id="my-link" href="http://example.com/"></a>

$('#my-link').on('click', function(e) {
    let originalUrl = this.getAttribute('href');
    let queryStringKey = 'someKey';
    let queryStringValue = encodeURIComponent(someDataObject);
    let updatedUrl = originalUrl + `?${queryStringKey}=${queryStringValue}`;
    e.originalEvent.currentTarget.href = updatedUrl;
    this.href = originalUrl; // 它随后打开原始URL而不是updatedUrl
});
英文:

I am trying to update or set a given URL specific query string value on a click of an &lt;a&gt; element.

I managed to make it work by setting the URL:

&lt;a id=&quot;my-link&quot; href=&quot;http://example.com/&quot;&gt;

$(&#39;#my-link&#39;).on(&#39;click&#39;, function(e) {
    let url = this.getAttribute(&#39;href&#39;);
    let queryStringKey = &#39;someKey&#39;;
    let queryStringValue = encodeURIComponent(someDataObject);
    let url += `?${key}=${queryStringValue}`;
    e.originalEvent.currentTarget.href = url; 
});

But this only works on the first click because it keeps appending the query string.

What I tried was to then save the original URL, set the url using e.originalEvent.currentTarget.href and then set it back after the click, but it doesn't work, it just reads the original url instead of the updated one:

&lt;a id=&quot;my-link&quot; href=&quot;http://example.com/&quot;&gt;

$(&#39;#my-link&#39;).on(&#39;click&#39;, function(e) {
    let originalUrl = this.getAttribute(&#39;href&#39;);
    let queryStringKey = &#39;someKey&#39;;
    let queryStringValue = encodeURIComponent(someDataObject);
    let updatedUrl += originalUrl + `?${key}=${queryStringValue}`;
    e.originalEvent.currentTarget.href = updatedUrl; 
    this.href = originalUrl; // it then opens this URL instead of the updatedUrl
});

答案1

得分: 1

使用URL API

注意:如果您没有有效的URL,可以使用new URL(this.href);,因为new URL(this.getAttribute("href"))href="/"上会失败。

$('#my-link').on('click', function(e) {
  e.preventDefault(); // 为了查看控制台
  let queryStringKey = 'someKey';
  let queryStringValue = { x: 2 };
  let url = new URL(this.href); 
  url.searchParams.set(queryStringKey, queryStringValue);
  this.href = url.toString();
  console.log(this.href)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="my-link" href="/">点击</a>
英文:

Use the URL API

Note: use new URL(this.href); if you do not have a valid URL since
new URL(this.getAttribute(&quot;href&quot;)) will fail on href=&quot;/&quot;
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;#my-link&#39;).on(&#39;click&#39;, function(e) {
  e.preventDefault(); // to see the console
  let queryStringKey = &#39;someKey&#39;;
  let queryStringValue = { x: 2 };
  let url = new URL(this.href); 
  url.searchParams.set(queryStringKey, queryStringValue);
  this.href=url.toString();
  console.log(this.href)
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;a id=&quot;my-link&quot; href=&quot;/&quot;&gt;Click&lt;/a&gt;

<!-- end snippet -->

答案2

得分: -1

每当单击URL时,删除附加的键值以恢复原始链接:

// A $( document ).ready() block.
$( document ).ready(function() {
    
    let someDataObject = 'someDataObject';
    $('#my-link').on('click', function(e) {
      e.preventDefault(); // to see the console
      let url = this.getAttribute('href');
     
      let queryStringKey = 'someKey';
      let queryStringValue = encodeURIComponent(someDataObject);

      url = url.split('?'+queryStringKey)[0];
  
      url += `?${queryStringKey}=${queryStringValue}`;
      
      console.log(url);
      e.originalEvent.currentTarget.href = url; 
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a id="my-link" href="http://example.com/">click me</a>

请注意,上述代码是用JavaScript和jQuery编写的,用于在单击链接时修改URL以删除附加的键值并恢复原始链接。

英文:

Whenever the url get clicked Remove the additional key value to restore the original link :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

// A $( document ).ready() block.
$( document ).ready(function() {
    
    let someDataObject = &#39;someDataObject&#39;;
    $(&#39;#my-link&#39;).on(&#39;click&#39;, function(e) {
      e.preventDefault(); // to see the console
      let url = this.getAttribute(&#39;href&#39;);
     
      let queryStringKey = &#39;someKey&#39;;
      let queryStringValue = encodeURIComponent(someDataObject);

      url = url.split(&#39;?&#39;+queryStringKey)[0];
  
      url += `?${queryStringKey}=${queryStringValue}`;
      
      console.log(url);
      e.originalEvent.currentTarget.href = url; 
  });
});

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;a id=&quot;my-link&quot; href=&quot;http://example.com/&quot;&gt;click me&lt;/a&gt;

<!-- end snippet -->

答案3

得分: -2

Sure, here is the translated JavaScript code:

$(&#39;#my-link&#39;).on(&#39;click&#39;, function(e) {
    let url = this.getAttribute(&#39;href&#39;).split(&#39;?&#39;)[0]; // 移除旧的查询字符串
    let queryStringKey = &#39;someKey&#39;;
    let queryStringValue = encodeURIComponent(&#39;someValue&#39;);
    url += `?${queryStringKey}=${queryStringValue}`;
    e.originalEvent.currentTarget.href = url; 
});
英文:

I would do so:

$(&#39;#my-link&#39;).on(&#39;click&#39;, function(e) {
    let url = this.getAttribute(&#39;href&#39;).split(&#39;?&#39;)[0]; // remove old query string
    let queryStringKey = &#39;someKey&#39;;
    let queryStringValue = encodeURIComponent(&#39;someValue&#39;);
    url += `?${queryStringKey}=${queryStringValue}`;
    e.originalEvent.currentTarget.href = url; 
});

huangapple
  • 本文由 发表于 2023年5月7日 19:38:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193690.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定