I want to add parameters to the destination URL just before moving with javascript.

Asked 1 years ago, Updated 1 years ago, 31 views

I would like to take over the conversion tag attached to the URL to the destination.
If it is a tag, it can be done by adding it to href in advance, but
It cannot be moved by JS.So I decided to adjust it just before moving.

I am trying to add parameters to the part of the URL just before moving with A tag or JS using the code below.
There are two things I want to realize.
1) I want to get the destination URL
2) I want to add parameters to the URL

1) I don't know how to do it now.

2) As for the following, I tried to add characters after canceling the event.
However,
A tag cannot be moved by itself
Moving in JS does not cancel (?) and moves to the first URL

I'm using beforeunload for general use, but is it impossible to use it?

I would appreciate it if you could let me know if there is a way to realize it.
Thank you for your cooperation.

<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8"/>
<title>jQuery TIPS</title>
</head>
<body>
<divid="outside">
  <p>
    <aid="inside" href="https://yahoo.co.jp/"> Page Move</a>
    <button id="otherUrl">Move Page</a>
  </p>
</div>

<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
window.addEventListener('beforeunload', function(e){
  e.preventDefault();
  e.stopPropagation();
  e.stopImmediatePropagation();
  window.location=window.location.href+"?2";
});
$(function(){
    $('#otherUrl').click(function(e){
        location = window.location.href+"?3";
    });
    $('a').click(function(e){
    e.preventDefault();
    window.location=location.href;+"?1";
  });
});
</script>
</body>
</html>

[Additional note]
As you can see in the comments, there are also "close" and "direct input", so there may be no way to "get the URL of the destination" in the first place.

javascript

2022-09-30 20:19

1 Answers

If you can't get a destination by moving the event, you can add the event to the element you want to move (a tag or button tag) and take the url of the destination from there

<!DOCTYPE html>
<html>
<head>
<metacharset="UTF-8"/>
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<divid="outside">
  <p>
    <aid="inside" href="https://yahoo.co.jp/"> Page Move</a>
    <button id="otherUrl">Move Page</button>
  </p>
</div>

<script>
$(function(){
  $('#otherUrl').click(function(e){
    e.preventDefault();
    location.href=location.href+"?3";
  });
  $('a').click(function(e){
    e.preventDefault();
    location.href=e.target.href+"?1";
  });
});
</script>
</body>
</html>


2022-09-30 20:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.