-1

Trying to do the following:

1) User is on page: https://www.example.net/value1/value2/value3/catid,xy somepages have value of catid=xy/value4/value5/value6/ - Category page

2) User clicks on LINK <a href="https://www.example.net/index.php?valuexyz&(******input value of cat id on the page im on now***)"></a>

I would like user on page catid,xy to click LINK and the URL string of catid,ab in the href to be automatically replaced with xy onclick.

Or redirected to page: https://www.example.net/index.php?valuexyz&catid,xy on click.

Simply put I want to have the URL string of catid change based on current page cat id.

      <script>
        if (window.location.href.indexOf("catid") > -1) {
        {
        document.getElementById("PostingAd").onclick HERE == window.location.href = 'https://www.example.net/index.php?value1value2value3&.catid';
        }</script>

I don't know how to include catid as it keeps changing on every page

NOTE: CATID is not necessarily xy or 12 or xyz it can be 1029 or 123 or hello etc.... also... Catid can be in format of "catid=" and "catid,"

UPDATE TO BE MORE CLEAR

they all have a cat id all i am asking is if im on URL 1 and i click on BUTTON x the button will direct me (the guy browsing on URL 1 = "www.example.com/blblblblblblblsaomasmadkasmdkfsdfsm/VALUE/slkfmsdlkfnlskdf" ) to URL b which is (www.example.com/kmfmksafmdalkf?dslf/VALUE/" for value to change based on url i am on

  • Try JavaScript's location.href = "url"; and just put in inside a function and pass the function inside the onclick or something - https://www.w3schools.com/jsref/prop_loc_href.asp – VirxEC Sep 18 '19 at 11:16
  • 1
    Welcome to SO! Please include the code that you have tested so far. What have you tried? We don't care if it doesn't work, because then this question wouldn't exist. – VirxEC Sep 18 '19 at 11:17
  • Added what I tried .... – Noah Smith Sep 18 '19 at 11:21
  • `&catid,ab` is not a valid URL search parameter - perhaps you mean `&catid=ab` ? – mplungjan Sep 18 '19 at 11:22
  • @RoryMcCrossan Thanks.... I did research for literally hours going from post to post before asking this question and couldn't find a solution with my limited knowledge of coding. I posted here what I tried and issue im having. – Noah Smith Sep 18 '19 at 11:23
  • I posted that comment as your question originally showed no effort on your part, nor any code for us to debug. I've removed it now, as you've edited the question to enable us to help you more effectively. – Rory McCrossan Sep 18 '19 at 11:24
  • @mplungjan Don't ask..........site is on Joomla 1.0 ..... Separate discussion but yes that is my URL structure. – Noah Smith Sep 18 '19 at 11:24
  • @RoryMcCrossan Thank you....wasn't trying to be rude....Just saying that much time was spent on my part before asking so it was kind of like......that's upsetting to read lol – Noah Smith Sep 18 '19 at 11:26
  • https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript – epascarello Sep 18 '19 at 12:32

1 Answers1

0

Something like this?

const siteURL = "https://www.example.net/value1/value2/value3/catid,xy/value4/value5/value6/"; // location.href
const siteCatId = siteURL.split("catid,")[1].split("/")[0];
document.getElementById("someContainer").addEventListener("click",function(e) {
  e.preventDefault(); // remove when live
  var tgt = e.target; 
  if (tgt.tagName==="A") {
    if (tgt.href.indexOf("&catid") !=-1) {
      tgt.href = tgt.href.replace(/catid([=|,]).*?$/g,function (match,oper) { 
      console.log(match,oper)
        return "catid"+oper+siteCatId;
      })
      console.log(tgt.href)
    }
  }
})
<div id="someContainer">
  <a href="https://www.example.net/index.php?valuexyz&catid=ab">CHANGES - catid=ab -> catid=xy</a><br/>
  <a href="https://www.example.net/index.php?valuexyz&catid,abc">CHANGES - catid,abc -> catid,xy</a><br/>
  <a href="https://www.example.net/index.php?valuexyz&nocatid">This link does not change</a>
</div>

NOTE if your URL has catid= then use URLSearchParams instead

mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • So i tried this code. Thank you very much. The issue is that it opens the url i put in the ahref....Also it's not a "," in second url its "=" i tried replaced it but doesnt work either. Keep in mind that in the ahref I cannot put a value to catid because i expect that value to change based on current url cat id – Noah Smith Sep 18 '19 at 11:43
  • You need to replace `const siteURL = "https://www.example.net/value1/value2/value3/catid,xy/value4/value5/value6/";` with `const siteURL = location.href;` AND make sure that your strings match the real world – mplungjan Sep 18 '19 at 11:44
  • Did you change someContainer to whatever is wrapping your links? Does the console show the correct link? – mplungjan Sep 18 '19 at 11:45
  • yep I did that....It doesn't work.....in the ahref i put catid= is that currect? as i need that value to change based on current location. – Noah Smith Sep 18 '19 at 11:47
  • No that is incorrect. It is not matching the spec we already discussed – mplungjan Sep 18 '19 at 11:47
  • I am replacing the catid,xx at the end of the url. If you change the format you need to change the regex – mplungjan Sep 18 '19 at 11:48
  • thanks so much for your help....I will try to play a bit now but heres the thing the catid url is: /catid,xy/ the url that redirected to is catid=xx at the end....The cat id changes on cat page when im there and when i click on the ahref i want catid of the current page to be there.... – Noah Smith Sep 18 '19 at 11:52
  • I changed the example - please change the question to reflect the format of the URLs – mplungjan Sep 18 '19 at 12:14
  • i changed the question to explain to the best of my abilities. I am trying the codes but they don't work. Redirects me to catid=ab or nocatid – Noah Smith Sep 18 '19 at 12:23
  • Did you SEE the above example? It redirects ONLY the first and it redirects that one to `https://www.example.net/index.php?valuexyz&catid=xy` - the second link is there to show it does NOT change because it does not HAVE a catid – mplungjan Sep 18 '19 at 12:25
  • they all have a cat id all i am asking is if im on URL 1 and i click on BUTTON x the button will direct me (the guy browsing on URL 1 = "www.example.com/blblblblblblblsaomasmadkasmdkfsdfsm/VALUE/slkfmsdlkfnlskdf" ) to URL b which is (www.example.com/kmfmksafmdalkf?dslf/VALUE/" for value to change based on url i am on – Noah Smith Sep 18 '19 at 12:27
  • You have to a) show the correct code and b) implement my code correctly - I cannot fix your code if your question is not correct – mplungjan Sep 18 '19 at 12:27
  • – Noah Smith Sep 18 '19 at 12:33
  • See update. I replace =ab with =xy and replace ,abc with ,xy – mplungjan Sep 18 '19 at 12:46
  • why are there 2 links? just 1 link. the current page i am on if value is hello in url ""catid=hello" or "catid,hello" Menaing if that is what is contained on the page i am actually on in the url. then the button should take me to another page but the value of catid change based on page i was on when i clicked the button.... the button is static on like 100 category pages. – Noah Smith Sep 18 '19 at 12:48
  • were not on the same page. idk how to explain it other than how i did. i tried....i do appreciate ur help – Noah Smith Sep 18 '19 at 12:49
  • I only have your explanations to work on and you have changed those explanations several times since the original question which still does not show in detail input and expected output - at least not in such a way it corresponds with your later narratives in your comments – mplungjan Sep 18 '19 at 12:51
  • This is for example SUPER unclear `UPDATE TO BE MORE CLEAR they all have a cat id all i am asking is if im on URL 1 and i click on BUTTON x the button will direct me (the guy browsing on URL 1 = "www.example.com/blblblblblblblsaomasmadkasmdkfsdfsm/VALUE/slkfmsdlkfnlskdf" ) to URL b which is (www.example.com/kmfmksafmdalkf?dslf/VALUE/" for value to change based on url i am on` – mplungjan Sep 18 '19 at 12:51
  • I am not a coder I tried to be as clear as I can....I put ur code just as you said and in the url i put catid=ab and it redirects me to literally catid=ab........it doesnt change it – Noah Smith Sep 18 '19 at 12:54
  • why is it so unclear .... URL of ahref go to a place with a catid which is the same in the url where u click from...... (the page ur currently on is "/catid=-,928384/" so /catid....../ of ahref changes to the same one that is on the current page..... just the catid/ section comma = + - if there is /catid.../ it replaces that on the url of ahref – Noah Smith Sep 18 '19 at 12:56
  • Because you now suddenly have VALUE inside the PATH but you wanted to change catid=VALUE or catid,VALUE depending on which comment I read – mplungjan Sep 18 '19 at 12:57
  • I copied your code word for word minus the things that obviously wouldnt work and it seem to work i put catid=ab just like ur example in ahref – Noah Smith Sep 18 '19 at 13:00
  • I am looking for `&catid` and not /catid/ so yeah – mplungjan Sep 18 '19 at 13:01