1

I have a lot of links I have to modify. I have to replace each url of them with a hardcoded link.

For example path/to/something.html?L into this/is/the/right/path.html?L

Everything until ?L has to be replaced. Everything in first path until the ?L parameter is dynamic.

How could I do this?

2 Answers 2

3

You can do it this way,

prev = $('a[href^=path/to/something.html]').attr('href')
required = prev.split('?')[1];

$('a[href^=path/to/something.html]').attr('href', 'this/is/the/right/path.html?' + required);
Sign up to request clarification or add additional context in comments.

1 Comment

You need to add the "?" to the new attribute or else it will just add the "required" to the href.
1

try this

$('a[href="path/to/something.html?L"]').attr('href', 'this/is/the/right/path.html?L')

Basically what it says is to find all the links with a certain href and replace the href with the new one.

Is L a constant or is it just for an example and L can be multiple things?

4 Comments

path/to/something.html can be any other link instead of the ?L parameter
Ok then Adil's answer will serve better.
But after die ?L can be more different parameters but i think this is a good breakpoint
then you need to split the href where the "?" is and replace only the first part with the new link. Adil's answer shows you how!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.