0

On my site, I want to search/replace for all instances of one URL (that has different IDs, otherwise it is the same) to another.

For example, my site could have the following hyperlinked URLs (they are just examples, again, the IDs will be different):

and so I would want the new URLs to be

I am using the following JavaScript code to achieve what I want in the HTML that follows, but it's not working. What am I doing wrong? How can the code be updated so the desired result is achieved?

<script type="text/javascript">
const old_url = "https://example.com/_layouts/15/listform.aspx?PageType=6&ListId=%7B6DDF7ABCX%2D5A27%2D4BEB%2DB4F9%2D158FAA3F0B34%7D&ID=14";

const searchParams = new URLSearchParams(old_url);
const ID = searchParams.get("ID");

const new_url = `https://example.com/Lists/Your%20Col/Item/editifs.aspx?ID=${ID}&Source=https://example.com/Lists/Your%20Col/AO12.aspx&DefaultView=AO`;

console.log(new_url);
    </script>
<html>

<a href="https://example.com/_layouts/15/listform.aspx?PageType=6&ListId=%7B6DDF7ABCX%2D5A27%2D4BEB%2DB4F9%2D158FAA3F0B34%7D&ID=7">This is a link</a>

<a href="https://example.com/_layouts/15/listform.aspx?PageType=6&ListId=%7B6DDF7ABCX%2D5A27%2D4BEB%2DB4F9%2D158FAA3F0B34%7D&ID=35">This is a link</a>

<a href="https://example.com/_layouts/15/listform.aspx?PageType=6&ListId=%7B6DDF7ABCX%2D5A27%2D4BEB%2DB4F9%2D158FAA3F0B34%7D&ID=100">This is a link</a>

<a href="https://example.com/_layouts/15/listform.aspx?PageType=6&ListId=%7B6DDF7ABCX%2D5A27%2D4BEB%2DB4F9%2D158FAA3F0B34%7D&ID=9434">This is a link</a>

</html>
Mike
  • 947
  • 2
  • 12
  • 18
  • what's wrong with it? is `new_url` not the correct value? – Chase Feb 11 '21 at 17:45
  • @chase the URL's in the – Mike Feb 11 '21 at 18:31
  • How do you expect them to change? Where in your code are you ever changing them? I only see a hardcoded `old_url` in your code, a correctly built `new_url` and a `console.log` - do you expect` console.log` to alter the html elements? – Chase Feb 11 '21 at 18:37
  • @chase that's a great point. i need to do something to the HTML elements, but I'm stuck and don't know what to do so they react as I need – Mike Feb 11 '21 at 18:42
  • Does this answer your question? [How to change the href for a hyperlink using jQuery](https://stackoverflow.com/questions/179713/how-to-change-the-href-for-a-hyperlink-using-jquery) – Chase Feb 11 '21 at 20:16

2 Answers2

0

did you try clearing the Cache, if not try clearing it? hopefully, it should work, Regards

0

The following may work for what you are trying to achieve here:

const old_url = "https://example.com/_layouts/15/listform.aspx?PageType=6&ListId=%7B6DDF7ABCX%2D5A27%2D4BEB%2DB4F9%2D158FAA3F0B34%7D&ID=14";

const searchParams = new URLSearchParams(new URL(old_url).search);
const ID = searchParams.get("ID");

const new_url = `https://example.com/Lists/Your%20Col/Item/editifs.aspx?ID=${ID}&Source=https://example.com/Lists/Your%20Col/AO12.aspx&DefaultView=AO`;
// Another way to do this
const new_url_alt = new URL('https://example.com/Lists/Your%20Col/Item/editifs.aspx?ID=${ID}&Source=https://example.com/Lists/Your%20Col/AO12.aspx&DefaultView=AO');
search_params_new.set('ID', ID);
new_url_alt.search = search_params_new;

console.log(new_url);
console.log(new_url_alt); // Same result

document.querySelectorAll(`a[href="${old_url}"]`).forEach(elem => elem.href = new_url); // Or new_url_alt; Both shall work
Param Siddharth
  • 858
  • 2
  • 7
  • 20